1
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.
- static void Main(string[] args)
- {
- var player = ChooseClass();
- if(player is Warrior)
- {
- var cType = ((Warrior)player).classtype;
- }
- else
-
- Console.Clear();
- Console.WriteLine();
- Console.ReadLine();
- }
Hope that works !
1
change this line
- Player player = ChooseClass();
to:
- var player = ChooseClass();
once you know by gettype which type of object is created, then you can write the logic accordingly
0
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.