List Info

Thread: C-Sharp (C#) Group: virtual and override confusion




C-Sharp (C#) Group: virtual and override confusion
user name
2006-01-02 18:42:40
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

C-Sharp (C#) Group: Re: virtual and override confusion
user name
2006-01-02 18:55:21
That example you gave would not even compile.  You must explicitly declare virtual/abstract methods as either public or protected, they cannot be private.  Once I declared CallingMember() as public, and the virtual and override MemberA() as protected, the example worked exactly as it should, it returned 2.

Having the base class in one library, and the derived class in another is not the issue, that is a common practice.  Here is what the classes looked like when I put them in a project and compiled them:

class BaseClass
{
   ;     public int CallingMember()
        {
            return MemberA();
        }
        protected virtual int MemberA()
        {
            return 1;
        }
}
class DerivedClass: BaseClass
{
   ;     protected override int MemberA()
        {
            return 2;
        }
}

BaseClass b = new DerivedClass();
this.output.Text = b.CallingMember().ToString();

On 1/2/06, Ernesto < equistangogmail.com">equistangogmail.com> wrote:

Hi,

I'm having a little confusion with inheritance in C#. Take this little
example:

class BaseClass {
 &nbsp;int CallingMember() {
 &nbsp; &nbsp;return MemberA();
 &nbsp;}
 &nbsp;virtual int MemberA() {
 &nbsp; &nbsp;return 1;
  }
}
class DerivedClass: BaseClass {
 &nbsp;override int MemberA() {
 &nbsp; &nbsp;return 2;
 &nbsp;}
}

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&quot; says:
&quot;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...&quot;. 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


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )