Trying to make a class based on user input, the problem I face is how to return created 'chosenHero' class? And so I could use that newly made class further on, adjusting properties and so on? I don't want to return a class from else
my Console App Game, GitHub project link is at the bottom.
Console.WriteLine("Would you like to play Mage, Warrior, Rogue or Warlock?");
private static object ChooseHero()
{
string answer = Console.ReadLine();
//Formating answer to lower
answer.ToLower();
//Changing class name to Title Case.
answer = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(answer);
if (answer == typeof(Mage).Name)
{
Console.WriteLine("You have chosen a " + answer);
Mage chosenHero = new Mage();
return chosenHero;
}
else if (answer == typeof(Warrior).Name)
{
Console.WriteLine("You have chosen a " + answer);
Warrior chosenHero = new Warrior();
return chosenHero;
}
else if (answer == typeof(Rogue).Name)
{
Console.WriteLine("You have chosen a " + answer);
Rogue chosenHero = new Rogue();
return chosenHero;
}
else if (answer == typeof(Warlock).Name)
{
Console.WriteLine("You have chosen a " + answer);
Warlock chosenHero = new Warlock();
return chosenHero;
}
else
{
Question($"Please try again... \nWould you like to play {nameof(Mage)}, {nameof(Warrior)}, {nameof(Rogue)} or {nameof(Warlock)}?");
Warlock IdontWantThis= new Warlock();
return IDontWantThis; //How to not return this part and Call ChooseHero method again?
}
}
my project on GitHub I am doing this mini studying project:
https://github.com/MentalRefinery/GitHub-Masters-Praktika-
Aucun commentaire:
Enregistrer un commentaire