Home All Groups Group Topic Archive Search About

A class structure question

Author
13 Aug 2006 12:00 AM
Sid Price
Hello,

I have a problem trying to figure out the following design issue. I have a
base class and a number of classes derived from that base. A method in the
base class covers 90% of the functionality required for all classes, however
I need to have that 10% functionality that is left in the derived classes.
What I would like to do is have the method in the base class be able to call
the method in the derived classes, it should do this according to the actual
class that was instantiated.

For example: The base class is "bClass", and there are two derived classes
"Class1" and "Class2". The method "Test" in bClass needs to call
"OtherMethod" in either Class1 or Class2. If I instantiate an object of type
Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this
be done?

Thanks,
Sid.

Author
13 Aug 2006 12:43 AM
Scott M.
How about using the "MyClass" keyword?


Show quoteHide quote
"Sid Price" <s**@nowhere.com> wrote in message
news:uYqjytmvGHA.4512@TK2MSFTNGP05.phx.gbl...
> Hello,
>
> I have a problem trying to figure out the following design issue. I have a
> base class and a number of classes derived from that base. A method in the
> base class covers 90% of the functionality required for all classes,
> however I need to have that 10% functionality that is left in the derived
> classes. What I would like to do is have the method in the base class be
> able to call the method in the derived classes, it should do this
> according to the actual class that was instantiated.
>
> For example: The base class is "bClass", and there are two derived classes
> "Class1" and "Class2". The method "Test" in bClass needs to call
> "OtherMethod" in either Class1 or Class2. If I instantiate an object of
> type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can
> this be done?
>
> Thanks,
> Sid.
>
>
Author
13 Aug 2006 4:58 AM
Tom Shelton
Sid Price wrote:
Show quoteHide quote
> Hello,
>
> I have a problem trying to figure out the following design issue. I have a
> base class and a number of classes derived from that base. A method in the
> base class covers 90% of the functionality required for all classes, however
> I need to have that 10% functionality that is left in the derived classes.
> What I would like to do is have the method in the base class be able to call
> the method in the derived classes, it should do this according to the actual
> class that was instantiated.
>
> For example: The base class is "bClass", and there are two derived classes
> "Class1" and "Class2". The method "Test" in bClass needs to call
> "OtherMethod" in either Class1 or Class2. If I instantiate an object of type
> Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this
> be done?
>
> Thanks,
> Sid.

It almost sounds like you need to create an abstract class (MustInherit
in VB.NET).  It would look something like:

Option Explicit On
Option Strict On

Imports System

Module Module1

    Sub Main()
        Dim bc1 As TheBaseClass = New ChildClass1
        Dim bc2 As TheBaseClass = New ChildClass2

        bc1.TheSubThatCallsTheMethod()
        bc2.TheSubThatCallsTheMethod()

    End Sub

    Private MustInherit Class TheBaseClass
        Protected MustOverride Sub TheAbstractMethod()


        Public Sub TheSubThatCallsTheMethod()
            ' do stuff
            Me.TheAbstractMethod()
            'do more stuff
        End Sub
    End Class

    Private Class ChildClass1
        Inherits TheBaseClass

        Protected Overrides Sub TheAbstractMethod()
            Console.WriteLine("Inside ChildClass1")
        End Sub
    End Class

    Private Class ChildClass2
        Inherits TheBaseClass

        Protected Overrides Sub TheAbstractMethod()
            Console.WriteLine("Inside ChildClass2")
        End Sub
    End Class

End Module

HTH

--
Tom Shelton [MVP]
Author
14 Aug 2006 12:22 AM
Sid Price
"Tom Shelton" <t**@mtogden.com> wrote in message
news:1155445098.677094.160760@74g2000cwt.googlegroups.com...
> It almost sounds like you need to create an abstract class (MustInherit
> in VB.NET).
> --
> Tom Shelton [MVP]
>

That looks like it does exactley what I need, thank you Tom.
Sid.
Author
13 Aug 2006 5:57 AM
Cor Ligthert [MVP]
Price,

Reading your message I had the same idea of Tom about the mustInherit.
However at the end I had the idea that this would not be always needed, you
are only asking about the possibilitie to override (as Tom shows nicely) or
to shadow members. However without that mustInherit is Tom's sample as well
very good.

See for Shadows this, you use it the same as overriding but the base class
is than not any more used at all for that member (be aware by this at what
level you use it, how you use interfaces and how you cast).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeyShadows.asp

I hope this helps,



Cor

Show quoteHide quote
"Sid Price" <s**@nowhere.com> schreef in bericht
news:uYqjytmvGHA.4512@TK2MSFTNGP05.phx.gbl...
> Hello,
>
> I have a problem trying to figure out the following design issue. I have a
> base class and a number of classes derived from that base. A method in the
> base class covers 90% of the functionality required for all classes,
> however I need to have that 10% functionality that is left in the derived
> classes. What I would like to do is have the method in the base class be
> able to call the method in the derived classes, it should do this
> according to the actual class that was instantiated.
>
> For example: The base class is "bClass", and there are two derived classes
> "Class1" and "Class2". The method "Test" in bClass needs to call
> "OtherMethod" in either Class1 or Class2. If I instantiate an object of
> type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can
> this be done?
>
> Thanks,
> Sid.
>
>
Author
13 Aug 2006 6:54 AM
Fabio
"Sid Price" <s**@nowhere.com> ha scritto nel messaggio
news:uYqjytmvGHA.4512@TK2MSFTNGP05.phx.gbl...

> For example: The base class is "bClass", and there are two derived classes
> "Class1" and "Class2". The method "Test" in bClass needs to call
> "OtherMethod" in either Class1 or Class2. If I instantiate an object of
> type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can
> this be done?

Define OtherMethod as abstract in bClass (that should be named ClassBase and
not bClass).
In Class1 and Class2 if you need special behavior do an override of
OtherMethod.


--

Free .Net Reporting Tool - http://www.neodatatype.net
Author
16 Aug 2006 3:56 PM
Phill W.
Sid Price wrote:

> I have a problem trying to figure out the following design issue. I have a
> base class and a number of classes derived from that base. A method in the
> base class covers 90% of the functionality required for all classes, however
> I need to have that 10% functionality that is left in the derived classes.
> What I would like to do is have the method in the base class be able to call
> the method in the derived classes, it should do this according to the actual
> class that was instantiated.
>
> For example: The base class is "bClass", and there are two derived classes
> "Class1" and "Class2". The method "Test" in bClass needs to call
> "OtherMethod" in either Class1 or Class2. If I instantiate an object of type
> Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this
> be done?

Two ways:
(1) If you really want two methods ("Test" and "OtherMethod"):

Class bClass
    Public Sub Test()
       ' 90% of test Code
       Me.OtherMethod()
    End Sub
    Public Overridable Sub OtherMethod()
       ' Do Nothing
    End Sub
End Class

Class Class1
    Public Overrides Sub OtherMethod()
       ' Do the other 10%
    End Sub
End Class

(2) Or, if you're not too bothered about having two methods, you can use
what I call "extending" (overriding, but then re-using the base class'
implementation), as in

Class bClass
    Public Overridable Sub Test()
       ' 90% of the job
    End Sub
End Class

Class Class1
    Public Overrides Sub Test()
       MyBase.Test() ' 90%

       ' Now, do the other 10%
    End Sub

End Class

HTH,
    Phill  W.
Author
16 Aug 2006 5:10 PM
Scott M.
> Two ways:
> (1) If you really want two methods ("Test" and "OtherMethod"):
>
> Class bClass
>    Public Sub Test()
>       ' 90% of test Code
>       Me.OtherMethod()
>    End Sub
>    Public Overridable Sub OtherMethod()
>       ' Do Nothing
>    End Sub
> End Class

Having a method that does nothing is a bad design idea.  It's like putting a
button on a remote control that does nothing.

> (2) Or, if you're not too bothered about having two methods, you can use
> what I call "extending" (overriding, but then re-using the base class'
> implementation), as in

Extending is what everyone else calls it as well.
Author
17 Aug 2006 12:58 PM
Phill W.
Scott M. wrote:
Show quoteHide quote
>> Two ways:
>> (1) If you really want two methods ("Test" and "OtherMethod"):
>>
>> Class bClass
>>    Public Sub Test()
>>       ' 90% of test Code
>>       Me.OtherMethod()
>>    End Sub
>>    Public Overridable Sub OtherMethod()
>>       ' Do Nothing
>>    End Sub
>> End Class
>
> Having a method that does nothing is a bad design idea.  It's like putting a
> button on a remote control that does nothing.

In itself, it does nothing, but it provides a "hook" for a derived class
to do something else .. er .. instead.

>> (2) Or, if you're not too bothered about having two methods, you can use
>> what I call "extending" (overriding, but then re-using the base class'
>> implementation), as in
>
> Extending is what everyone else calls it as well.

That's a relief.  There's enough terminology around already without me
inventing any more of it   ;-)

Regards,
    Phill  W.
Author
17 Aug 2006 3:07 PM
Scott M.
>> Having a method that does nothing is a bad design idea.  It's like
>> putting a button on a remote control that does nothing.
>
> In itself, it does nothing, but it provides a "hook" for a derived class
> to do something else .. er .. instead.

This is still a bad design interface.  Since we don't know *if* a derived
class will even want to do *something* with this method, we are adding a
method for the sake of adding a method.