How do you call a child objects method from the parent

  • Thread starter Thread starter jw56578
  • Start date Start date
J

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

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

Back
Top