Code in derived class not called

G

Guest

Hi

Please can someone explain this behaviour:

I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.

If I Dim a variable of type Derived and New it as Derived the code in the
Derived class is called. However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code in
the derived class to be called in all instances.

Public MustInherit Class Base
Public Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Shadows Sub DoSomething()
MsgBox("Class1")
End Sub
End Class


Dim x As Base = New Class1
x.DoSomething() ' "Base" displayed

Dim y As Class1 = New Class1
y.DoSomething() ' "Class1" displayed

Many thanks for your comments.

Julia.
 
I

Imran Koradia

That is correct behaviour. Think about it - if your base class didn't have
the DoSomething method, would you even have been able to call DoSomething
via the base class variable? Definitely not - only if you cast it to the
derived class would you be able to do that. The base class variable can and
will only access members in the base class even though the object is an
instance of the derived class.

hope that helps..
Imran.
 
J

JD

Look into Overridable and Overrides, this will give you the behavior you are
looking for.
 
J

Jay B. Harlow [MVP - Outlook]

Julia,
I have a MustInherit Base class and a Derived class that Inherits Base and
Shadows a method in the base class.
There's the rub!
However, if I Dim the variable as type Base but New
it as type Derived the Base class code is called - I would expect the code
in
the derived class to be called in all instances.

To get the behavior you want you need to use Overrides in the Derived class.
In the base class you need to use Overridable or MustOverride.

Something like:
Public MustInherit Class Base
Public Overridable Sub DoSomething()
MsgBox("Base")
End Sub
End Class

Public Class Class1
Inherits Base

Public Overrides Sub DoSomething()
MsgBox("Class1")
End Sub
End Class

Shadows is used when you define DoSomething in Class1, then later you add
DoSomething in Base, where Base.DoSomething is not compatible with
Class1.DoSomething, Shadows allows both to exist without interfering with
each other. In other words so that Base.DoSomething can be called without
invoking Class1.DoSomething.

Overridable is used when you offer implementation in Base that Derived can
replace or supplement.
MustOverride is used when Base has no implementation that Derived has to
implement.

For a very good "How To" book on OO in VB.NET check out Robin A.
Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and Microsoft
Visual C# .NET - Step by Step" from MS Press.

Hope this helps
Jay
 
G

Guest

Hi Julia

As others have said, the behaviour you are seeing is correct, and Overrides
/ Overridable will give you the behaviour you want.

In terms of the why, let's have a go at that.

A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the
Me / this) pointer (oops reference). So when using a reference to a type, you
will always call this global function, no matter what the object is that is
being referenced.

In contrast, an Overridable / virtual method is implemented as a virtual
function table in the object itself. Calling a virtual / overridable method
looks up the implementation from the object indirectly, rather than calling
the function directly.

HTH

Nigel Armstrong
 
J

JD

A regular method (as in not an Overridable or virtual method) in a .NET
class is implemented as a global function with a hidden first argument (the
Me / this) pointer (oops reference). So when using a reference to a type, you
will always call this global function, no matter what the object is that is
being referenced.

I don't know if "Global" is a good word to use here. Its my understanding
that each type has a method table, first region within the method table is
for virtual methods, the second region for non-virtual methods. So the
methods are not "Global", and there really isn't a virtual method table,
there is just a method table. The methods are only relevant within the
specific type or its derived types.

Also for the hidden first argument, that happens whether the method is
virtual or not. I don't know if you meant that it only happens with the
non-virtual method.

JD
 

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