Hi,
I'm having a little confusion with inheritance in C#. Take
this little
example:
class BaseClass {
int CallingMember() {
return MemberA();
}
virtual int MemberA() {
return 1;
}
}
class DerivedClass: BaseClass {
override int MemberA() {
return 2;
}
}
BaseClass myobject;
myobject = new DerivedClass();
Console.WriteLine(myobject.CallingMember().ToString());
The result is 1, and I expect it to be 2 as the
documentation for
"virtual" says:
"When a virtual method is invoked, the run-time type of
the object is
checked for an overriding member. The overriding member in
the most
derived class is called...". Even the documentation
example says it
should be 2. A step by step debugging confirms that the
object is of
type DerivedClass, but MemberA for BaseClass is called.
The problem seems to be that BaseClass is declared in a
class lib,
while DerivedClass is declared in another class library and
the code
declaring myobject is in a Windows app (referencing both
libs).
So, how am I supposed to do this?
Thanks in advance,
Ernesto
|