Inheritance question - VB.NET 2005

S

Sid Price

I have a problem with my class hierarchy at runtime and would appreciate
some input.
I have a base class, let's call it "BaseClass", that has a public function
"ToXML". In BaseClass.ToXML some common parameters are written to an XML
document and the document is returned. In the derived classes I have an
overloaded function that is designed to call the base class to create and
write the common elements to the XML document and then each derived class
writes their elements.
There are also some classes derived from these sub-classes. So we have:
BaseClass -> SubClass_1 -> SubClass_2.

In my application I have a method that may receive anyone of the class
objects in my hierarchy of classes derived from BaseClass so it looks a bit
like this:

public sub WriteOutput(byval oMessage as BaseClass)
oMessage.ToXML()
end sub

What I expect to happen is that late binding will cause the ToXML method in
a derived class to be called. So calling WriteOutput with a SubClass_2
object I expected the SubClass_2.ToXML to be called. What I find is that
BaseClass.ToXML is always called.
I have tried declaring the ToXML methods of derived class both with
"overloads" and with "shadows" and still BaseClass.ToXML is called on all
passed objects.
I would appreciate any help in understanding the correct way to build this
class structure.
Thanks,
Sid.
 
K

Kerry Moorman

Sid,

This should be a pretty straightforward use of inheritance.

You should mark the baseclass's ToXML method as being overridable:

Public Overridable Function ToXML() As ...

Then you should mark each subclass's ToXML method as overrides:

Public Overrides Function ToXML() As ...

If the ToXML method in a subclass needs to call the baseclass's ToXML method
then you should use the MyBase keyword.

Kerry Moorman
 
R

rowe_newsgroups

I have a problem with my class hierarchy at runtime and would appreciate
some input.
I have a base class, let's call it "BaseClass", that has a public function
"ToXML". In BaseClass.ToXML some common parameters are written to an XML
document and the document is returned. In the derived classes I have an
overloaded function that is designed to call the base class to create and
write the common elements to the XML document and then each derived class
writes their elements.
There are also some classes derived from these sub-classes. So we have:
BaseClass -> SubClass_1 -> SubClass_2.

In my application I have a method that may receive anyone of the class
objects in my hierarchy of classes derived from BaseClass so it looks a bit
like this:

public sub WriteOutput(byval oMessage as BaseClass)
    oMessage.ToXML()
end sub

What I expect to happen is that late binding will cause the ToXML method in
a derived class to be called. So calling WriteOutput with a SubClass_2
object I expected the SubClass_2.ToXML to be called. What I find is that
BaseClass.ToXML is always called.
I have tried declaring the ToXML methods of derived class both with
"overloads" and with "shadows" and still BaseClass.ToXML is called on all
passed objects.
I would appreciate any help in understanding the correct way to build this
class structure.
Thanks,
Sid.

The following code sample should show you the proper way of doing this
(it's a console app if you want to copy/paste and play around with it)

//////////////////////
Option Strict On

Module Module1

Sub Main()
Dim baseClass As New BaseClass()
Dim childClass As New ChildClass()
Dim grandChildClass As New GrandChildClass()

WriteOutput(baseClass)
WriteOutput(childClass)
WriteOutput(grandChildClass)

Console.Read()
End Sub

Public Sub WriteOutput(ByVal baseClass As BaseClass)
baseClass.WriteOutput()
End Sub

End Module

Public Class BaseClass

Public Overridable Sub WriteOutput()
Console.WriteLine("Output from BaseClass")
End Sub

End Class

Public Class ChildClass
Inherits BaseClass

Public Overrides Sub WriteOutput()
Console.WriteLine("Output from ChildClass")
End Sub

End Class

Public Class GrandChildClass
Inherits ChildClass

Public Overrides Sub WriteOutput()
Console.WriteLine("Output from GrandChildClass")
End Sub

End Class
//////////////////////

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
S

Sid Price

Kerry Moorman said:
Sid,

This should be a pretty straightforward use of inheritance.

You should mark the baseclass's ToXML method as being overridable:

Public Overridable Function ToXML() As ...

Then you should mark each subclass's ToXML method as overrides:

Public Overrides Function ToXML() As ...

If the ToXML method in a subclass needs to call the baseclass's ToXML
method
then you should use the MyBase keyword.

Kerry Moorman
Yes indeed that works, my confusion between "overload" and "override". Thank
you,
Sid.
 
S

Sid Price

rowe_newsgroups said:
The following code sample should show you the proper way of doing this
(it's a console app if you want to copy/paste and play around with it)

Thank you Seth, as I said in response to the other reply I misunderstood the
use of "overload" and "override". Your suggestion was right-on.
Sid.
 

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