3
Answers

Trying to make a ChooseClass() method and return the type.

Joshua Hecht

Joshua Hecht

7y
252
1
I'm new to C# programming and attempting to make a basic Text Adventure style console application, and I am having trouble using user input to choose the class they want.
 
I've created the following classes
 
public class Player
{
   public WoodenStick firstWeapon = new WoodenStick(); //Ignore for now
}
public class Warrior : Player
{
   public string classType = "Warrior";
}
public class Sorcerer : Player
{
   public string classType = "Sorcerer";
}
 
Though my problem lies within the Main method of the program...
 
I created a method inside of the Program class called ChooseClass() which I want to get user input and create a class based off of what the user inputs.
 
See below
 
class Program
{
   static void Main(string[] args)
{
      Player player = ChooseClass();
      Console.Clear();
      Console.WriteLine();
      Console.ReadLine();
}
   static Player ChooseClass()
   {
      Warrior warrior = new Warrior();
      Sorcerer sorcerer = new Sorcerer();
ChooseClass:
      Console.WriteLine("Choose a class: Warrior or Sorcerer");
      Console.WriteLine();
      string response = Console.ReadLine();
      if (response.Equals("Warrior"))
         return warrior;
      else if (response.Equals("Sorcerer"))
         return sorcerer;
      else
      {
         Console.Clear();
         Console.WriteLine("Try again");
         Console.ReadLine();
         Console.Clear();
         goto ChooseClass;
      } 
    }
}
 
 
Hopefully the code is readable to you. The method kind of works. When I do player.GetType(), it does return either Sorcerer or Warrior, but I can't access the individual properties of Sorcerer or Warrior because the actual object is defined as Player. How can I get it to create the specific object based on the user input?
 
Thanks for the help! 
 
 
 
Answers (3)
1
Amit Gupta
NA 16.5k 25.7k 7y
Hey
 
As your base class is Player which was derived by  Warrior and Sorcerer class, just cast your Player class to the respective derive class to access its field.
 
 
  1. static void Main(string[] args)    
  2. {    
  3.       var player = ChooseClass();    
  4.       if(player is Warrior)    
  5.       {  
  6.          var cType = ((Warrior)player).classtype;  
  7.       }    
  8.       else    
  9.         // Do your rest stuff, same as above to cast to access its variable  
  10.       Console.Clear();    
  11.       Console.WriteLine();    
  12.       Console.ReadLine();    
  13. }    
 
Hope that works ! 
1
Nilesh Shah
NA 22.3k 215k 7y
change this line
  1. Player player = ChooseClass(); 
 
to:
  1. var player = ChooseClass(); 
 
once you know by gettype which type of object is created, then you can write the logic accordingly
0
Joshua Hecht
NA 8 629 7y
Hi Nilesh,
 
I've switched type Player to var in the main method, but I'm still in the same situation because the return type of the method is still Player, and it won't accept var as the return type. Therefore, I still can't access the fields of the Warrior/Sorcerer class because it thinks its just a Player.