Addressing Virtual Method in a Base Class

G

Guest

In the following code, calling CallTestMethod() from an instance of the
derived class (the base is abstract and we can never instantiate it),
referencing 'base.TestMethod()' actually calls 'TestMethod()' in the base
class, but referencing 'this.CallTestMethod()' (defined in the base class)
references the overriding method, not the method defined within the base
class, making calling 'TestMethod()' seem virtually impossible (pun intended).

So my question is: Is there another way to call a virtual method defined in
the base class from the base class itself?

(note: I know that a work-around is to put the code from the virtual method
into another method and call that, but I am asking the question to understand
the language better.)


abstract public class TestObjectBase
{
virtual public void TestMethod()
{
// do stuff
}

public string CallTestMethod()
{
TestMethod(); // calls the method from the derived class
this.TestMethod(); // also call the method from the derived class
// Question: Is there any way to call the TestMethod defined above?
}
} // end of class


public class TestObject : TestObjectBase
{
public override void TestMethod() {
// do other stuff
}

public void CallBaseTestMethod() {
base.TestMethod(); // this references the method defined in the
base class
}
}
 
M

Mattias Sjögren

So my question is: Is there another way to call a virtual method defined in
the base class from the base class itself?

No there isn't, not in C#. Other languages such as VB and C++ lets you
do it though.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top