I'm trying to get the property from an inherited class without creating a new instance and rather use the parameter being passed in the method.
How can I get the property Color over here?
Does not work:
- public void InitPlay(Toy toy)
- {
- Console.WriteLine($" The {TypeOfAnimal}{Name} is now playing with the {toy.Color} {toy} ");
- }
Works:
This one works. But that's just because I created a new object of class Ball. But the point of passing in toy is thus redundant.
- public void InitPlay(Toy toy)
- {
- Ball ball = new Ball();
- Console.WriteLine($" The {TypeOfAnimal}{Name} is now playing with the {ball.Color} {toy} ");
- }
Rest of the code (if needed)
- class Ball : Toy
- {
- public string Color { get; set; }
- }
- abstract class Toy
- {
- ...
- }