473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code in derived class not called

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.
Nov 21 '05 #1
10 1373
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.

"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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.

Nov 21 '05 #2
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.

"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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.

Nov 21 '05 #3
JD
Look into Overridable and Overrides, this will give you the behavior you are
looking for.
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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.

Nov 21 '05 #4
JD
Look into Overridable and Overrides, this will give you the behavior you are
looking for.
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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.

Nov 21 '05 #5
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
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com... 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.

Nov 21 '05 #6
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
"Julia" <Ju***@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com... 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.

Nov 21 '05 #7
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

"Julia" wrote:
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.

Nov 21 '05 #8
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

"Julia" wrote:
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.

Nov 21 '05 #9
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
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:4C**********************************@microsof t.com... 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

"Julia" wrote:
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.

Nov 21 '05 #10
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
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:4C**********************************@microsof t.com... 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

"Julia" wrote:
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.

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11805
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
3
2907
by: Elia Karagiannis | last post by:
The static constructor of a derived class never gets called if it has no other methods and the base class is only static I have the following base class and derived class. public class MyBase...
7
3486
by: Jerry Nixon | last post by:
I hope this is easy, First, I simply must not override MyMethod as shown in the sample code - that is an unbreakable requirement. The purpose of the event is to allow me to extend MyMethod...
0
824
by: Julia | last post by:
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...
5
1719
by: dbuchanan | last post by:
Hello, I can find no reason for this code to run twice. Can you help me? It may have something to do with it being a overrides sub but stepping through during debug does *not* bear this out. ...
6
2465
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
8
2206
by: Kannan | last post by:
Some amount of memory is allocated inside the Base Constructor using new. During the construction of a derived object an exception occurred in the constructor of the derived class. Will the...
9
1937
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should...
8
1506
by: Pallav singh | last post by:
#include<iostream.h> #include<memory.h> #include<string.h> // product class Pizza { private : std::string Topping;
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.