Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made a class named Wizard with some basic stuff like name and spell slots and experience and I have this

C#
Wizard wizard01 = new Wizard();

And I want the user to choose between wizard01 and wizard02 by turning them into numbers.

So if the user wants wizard01 he enters 1 but after that I want the data for wizard01 to be used like the name of the wizard01.

What I have tried:

I didn't try anything. I can't figure it out.
Posted
Updated 25-Jan-22 6:41am
v3

1 solution

If you put all your instances of the Wizard class in an array, you can pick one by indexing that array; that would result in translating an integer number (zero-based) into one of a range of pre-instantiated wizards, like so:

C#
Wizard[] wizards=new Wizard[10];
wizards[0]=new Wizard(...);
...
wizards[9]=new Wizard(...);

// and then

int myWizIndex=4;
Wizard myWizard=wizards[myWizIndex];
...


FYI: if you don't know the number of wizards you're going to have (you might create them dynamically) and/or if their integer indexes aren't necessarily a continuous and zero-based range, you could also use a Dictionary<int,Wizard> (that is a more advanced topic!).

:)
 
Share this answer
 
Comments
BlobifyYT 25-Jan-22 2:43am    
thats my script can you edit it if you want thx







using System;

namespace Classes
{
class Wizard
{
public string name;
public string favSpell;
private int spellSlots;
private float experience;

public Wizard(string _name, string _favSpell)
{
name = _name;
favSpell = _favSpell;

spellSlots = 3;
experience = 0f;
}

public void CastSpell()
{
if (spellSlots > 0)
{
Console.WriteLine(name + " Casts " + favSpell);
spellSlots--;
experience+= 0.3f;
}
else
{
Console.WriteLine(name + " Is Out Of Spells");
}
}

public void Meditate()
{
Console.WriteLine(name + " Meditates To Regain Slots.");
spellSlots = 3;
}
}


class Program
{
static void Main(string[] args)
{
Wizard wizard01 = new Wizard("Alberto Mertano", "Spectrum Potrumo");
Wizard wizard02 = new Wizard("Detrono Ceranto", "Extreme Bispheno");

Console.WriteLine("Choose A Character : ");
Console.WriteLine(" Alberto Mertano Detrono Ceranto");


//Wait Before Closing
Console.ReadKey();
}
}
}
BlobifyYT 25-Jan-22 3:07am    
Nevermind I Found A Simple Way

String wizChoose = Console.ReadLine( );
If (wizChoose = "wizard01")
{
Console.WriteLine(" You Chose " + wizChoose);
}
Else if(wizChoose = "wizard02")
{
Console.WriteLine(" You Chose " + wizChoose);
}
Else
{
Console.WriteLine(" This Is Not A Valid Character");
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900