Home All Groups Group Topic Archive Search About

How do you call a child objects method from the parent

Author
13 Apr 2005 10:58 PM
jw56578
Is it possible to call a method from a parent object, but have the
childs overriden method called instead of the base method?
I want several children objects to all call a certain method when they
are initialized, but they all have different implementations of that
method.
thanks

Author
13 Apr 2005 11:19 PM
Tom Shelton
In article <1113433100.410067.110***@g14g2000cwa.googlegroups.com>, jw56***@gmail.com wrote:
> Is it possible to call a method from a parent object, but have the
> childs overriden method called instead of the base method?
> I want several children objects to all call a certain method when they
> are initialized, but they all have different implementations of that
> method.
> thanks
>

Two things...  If your talking about constructors - then, it doesn't
matter because constructors are not inherited.  So, each child class
will have it's own unique Sub New.

If your talking about a method that is called from a parent reference,
then you can do something like this:

Option Strict On
Option Explicit On

Module Module1

    Sub Main()
        Dim p As Parent

        p = New Child1
        p.Method()

        p = New Child2
        p.Method()

        p = New Child3
        p.Method()
    End Sub

    Private MustInherit Class Parent
        Public MustOverride Sub Method()
    End Class

    Private Class Child1
        Inherits Parent

        Public Overrides Sub Method()
            Console.WriteLine("Child1")
        End Sub
    End Class

    Private Class Child2
        Inherits Parent

        Public Overrides Sub Method()
            Console.WriteLine("Child3")
        End Sub
    End Class

    Private Class Child3
        Inherits Parent

        Public Overrides Sub Method()
            Console.WriteLine("Child3")
        End Sub
    End Class

End Module

If none of this actually answers your question...  Hopefully someone
else will understand what your asking better :)  Or, you can post some
air-code that sort of shows off what you're trying to do.

--
Tom Shelton [MVP]
Author
13 Apr 2005 11:35 PM
Tom Shelton
In article <uFeFA9HQFHA.***@TK2MSFTNGP12.phx.gbl>, Tom Shelton wrote:
Show quoteHide quote
> In article <1113433100.410067.110***@g14g2000cwa.googlegroups.com>, jw56***@gmail.com wrote:
>> Is it possible to call a method from a parent object, but have the
>> childs overriden method called instead of the base method?
>> I want several children objects to all call a certain method when they
>> are initialized, but they all have different implementations of that
>> method.
>> thanks
>>
>
> Two things...  If your talking about constructors - then, it doesn't
> matter because constructors are not inherited.  So, each child class
> will have it's own unique Sub New.
>
> If your talking about a method that is called from a parent reference,
> then you can do something like this:
>
> Option Strict On
> Option Explicit On
>
> Module Module1
>
>     Sub Main()
>         Dim p As Parent
>
>         p = New Child1
>         p.Method()
>
>         p = New Child2
>         p.Method()
>
>         p = New Child3
>         p.Method()
>     End Sub
>
>     Private MustInherit Class Parent
>         Public MustOverride Sub Method()
>     End Class
>
>     Private Class Child1
>         Inherits Parent
>
>         Public Overrides Sub Method()
>             Console.WriteLine("Child1")
>         End Sub
>     End Class
>
>     Private Class Child2
>         Inherits Parent
>
>         Public Overrides Sub Method()
>             Console.WriteLine("Child3")
>         End Sub
>     End Class
>
>     Private Class Child3
>         Inherits Parent
>
>         Public Overrides Sub Method()
>             Console.WriteLine("Child3")
>         End Sub
>     End Class
>
> End Module
>
> If none of this actually answers your question...  Hopefully someone
> else will understand what your asking better :)  Or, you can post some
> air-code that sort of shows off what you're trying to do.
>

--
Tom Shelton [MVP]