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.

RE: Inheritance question - VB.NET 2005 by KerryMoorman

KerryMoorman
Tue Jul 22 19:55:03 CDT 2008

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


"Sid Price" wrote:

> 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.
>
>
>

Re: Inheritance question - VB.NET 2005 by rowe_newsgroups

rowe_newsgroups
Wed Jul 23 06:20:15 CDT 2008

On Jul 22, 8:35=A0pm, "Sid Price" <s...@nowhere.com> wrote:
> 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 functio=
n
> "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 b=
it
> like this:
>
> public sub WriteOutput(byval oMessage as BaseClass)
> =A0 =A0 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 thi=
s
> 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/

Re: Inheritance question - VB.NET 2005 by Sid

Sid
Wed Jul 23 10:05:35 CDT 2008

>"rowe_newsgroups" <rowe_email@yahoo.com> wrote in message
>news:d9c24102-d4a9-40cd-8820->4ecf4d202e5d@w7g2000hsa.googlegroups.com...
>On Jul 22, 8:35 pm, "Sid Price" <s...@nowhere.com> wrote:
>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.



Re: Inheritance question - VB.NET 2005 by Sid

Sid
Wed Jul 23 10:03:49 CDT 2008

"Kerry Moorman" <KerryMoorman@discussions.microsoft.com> wrote in message
news:0B9329ED-865B-4E7D-B7D6-C7DBA926D0FA@microsoft.com...
> 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.