• 12
• 12
• 9
• 10
• 13

C# confusion [Solved]

This topic is 4590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I just ran across something that doesn't seem to make sense to me but that works and I would like to know how it's possible and what could be the possible consequences from it. I have been testing the inheritance features of C# such as constructors overloading over multiple derived classes and then I found something weird due to a mistake I made. Let's say I have the namespace Game.Engine.Init I have made an abstract class called InitGame, and a derived class InitControls inheriting InitGame. In both classes I put some bogus variables to test and I found out that when I call this: Game.Engine.Init.InitGame j00 = new Game.Engine.Init.InitControls(); this works for some unknown reasons and also I cannot use the variables in the InitControls Class. So I assumed that I specified of the class as InitGame using the InitControls constructor which first calls the InitGame CTor but I don't know how it can work after that because it's suposed to continue on with the InitControls CTor which would make no sense since the variables from the class are not avalaible in the base class. Any idea guys ? This is weird and I need to know what happened there. [Edited by - Avenyr on August 27, 2005 5:30:41 PM]

Share on other sites
I'm not quite following. Could you show the code for your situation?

Share on other sites
Here is the code

using System;namespace Game.Engine.Main{	public class EntryPoint	{		static void Main()		{			Game.Engine.Init.InitControls test  = new Game.Engine.Init.InitControls(10); //Works like it should			Game.Engine.Init.InitGame     test2 = new Game.Engine.Init.InitControls(20); //Works somehow for reasons I ignore yet			Console.WriteLine();			Console.WriteLine(test.InitBogus);			Console.WriteLine();						Console.WriteLine(test.ControlBogus);						Console.WriteLine();						Console.WriteLine(test2.InitBogus);			//Console.WriteLine(test2.ControlBogus); Obviously does not work since InitGame does not contain ControlBogus			Console.Read();		}	}}namespace Game.Engine.Init{	public abstract class InitGame	{		public int InitBogus;			public InitGame()		{			Console.WriteLine("Init Game CTor called");			InitBogus = 50;		}	}	public class InitControls : InitGame	{		public int ControlBogus;		public InitControls(int number)		{			Console.WriteLine("Init Controls CTor called");			ControlBogus = number;		}	}}

This line confuses me : Game.Engine.Init.InitGame test2 = new Game.Engine.Init.InitControls(20); //Works somehow for reasons I ignore yet

Share on other sites
It works because its still legal to have a reference of an abstract base type; you can fill it with a reference to an object of any derived class.

Share on other sites
InitControls is a InitGame, that is what inheritance does. The reason you can't access the InitControls variables is because you are using it as an InitGame, and can only access what is defined as part of InitGame. Other than this, I'm, not sure what you are asking about, so try clarifying.

Share on other sites
What I want to know is why does the InitControl Constructor works when the base class is specified because the line in the InitControl CTor where it sets the ControlBogus variable should fail since the base class does not contain this variable.

I understand that since the object is of the base class and not the derived class that I cannot access the derived class' variable.

Share on other sites
When you have a type declaration on the left hand side of the assignment has no impact on what the right hand side does. The right hand side in this case creates an object of the derived class and then assigns a reference to that derived class to a base class reference. The derived class does have that variable so all is good.

Share on other sites
So basically the extra data not contained in the base class gets discarded when I assign a derived class variable to a base class variable ?

Share on other sites
No, it stays around. Why would it get discarded? You're assigning references, not value types.