I do have a homework problem that I am having trouble with. I am new to programing, so I will explain this the best I can.
ClassB is derrived from ClassA and has the same method names as ClassA. One is overriden and one is new. Class A has one virtual function that can be overriden.
I want to set string example5 to the string in InstanceB.TestB. "TestB from ClassB" I have tried casting IntanceB to class B, but am not getting any where with that. I also have tried using the is and as operators and am getting errors. I keep getting the string equal to "TextB from ClassA".
The classes are initiated the way they are supposed to be an I believe it has something to do with that.
ClassA InstanceB = new ClassB();
Any pointers in the right direction or help understanding why you would initiate InstanceB in such a way would be appreciated.
Code:namespace TestIt { class Program { class ClassA { public virtual string TestA() { return "TestA from ClassA"; } public string TestB() { return "TestB from ClassA"; } public virtual string TestC() { return "TestC from ClassA"; } } class ClassB : ClassA { public override string TestA() { return "TestA from ClassB"; } new public string TestB() { return "TestB from ClassB"; } } static void Main(string[] args) { ClassA InstanceA = new ClassA(); ClassA InstanceB = new ClassB(); ClassB InstanceC = new ClassB(); object p = InstanceC; InstanceB = p as ClassB; string example5 = InstanceB.TestB(); } } }