How to have New() in a Base Class create and return an instance of a Derived Class?

J

Joe HM

Hello -

I have a Base Class where I want a New() implemented that can be called
from the outside. This New() should create an instance of the
appropriate cDerivedX Class ...

The following shows a scenario where it was determined that cDerivedA
is needed based on a configuration file.

Dim lInstance As New cBase()

This statement should then create an instance of ...

Class cDerivedA
Inherits cBase
...
End Class

During a different run the configuration file requires cDerivedB being
used ...

Dim lInstance As New cBase()

This statement should now create an instance of ...

Class cDerivedB
Inherits cBase
...
End Class

So I was trying to tweak the Public Sub New() in cBase. How can I call
the New() of the appropriate cDerivedX Class and create an instance of
it that will be accessed through lInstance?

Thanks a lot!
Joe
 
H

Herfried K. Wagner [MVP]

Joe HM said:
I have a Base Class where I want a New() implemented that can be called
from the outside. This New() should create an instance of the
appropriate cDerivedX Class ...

The following shows a scenario where it was determined that cDerivedA
is needed based on a configuration file.

Dim lInstance As New cBase()

This statement should then create an instance of ...

Class cDerivedA
Inherits cBase
...
End Class

During a different run the configuration file requires cDerivedB being
used ...

Dim lInstance As New cBase()

This statement should now create an instance of ...

Class cDerivedB
Inherits cBase
...
End Class

So I was trying to tweak the Public Sub New() in cBase. How can I call
the New() of the appropriate cDerivedX Class and create an instance of
it that will be accessed through lInstance?

That's not possible. You may want to take a look at the Factory design
pattern.
 
J

Joe HM

Hello -

Thanks for your response. I used to do something like that in C++
where I could acutally return something for the new().

How can I do the Factory Design Pattern in VB.NET?

Thanks again,
Joe
 
J

Jay B. Harlow [MVP - Outlook]

Joe,
As Herfried suggests, I would recommend a factory method/pattern.

Something like:

' don't allow this class to be instantiated directly
Public MustInherit Class Base

' don't allow this class to be instantiated directly
Protected Sub New
End Sub

Public Shared Function Create() As Base
' read config file
Select Case configSetting
Case A
Return New DerivedA
Case B
Return New DerivedB
Case ...
End Select
End Function

End Class

Public Class DerivedA
Inherits Base

Friend Sub New()
End Sub

End Class

Public Class DerivedB
Inherits Base

Friend Sub New()
End Sub

End Class

Then to use it you use:

Dim lInstance As Base = Base.Create()

Instead of a Select Case I have used Type.GetType coupled with
Activator.CreateInstance to create an instance of the derived class...

NOTE: The "Shared" in "Public Shared Function Create" allows the Create
function to be called without creating an instance of the Base class first.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello -
|
| I have a Base Class where I want a New() implemented that can be called
| from the outside. This New() should create an instance of the
| appropriate cDerivedX Class ...
|
| The following shows a scenario where it was determined that cDerivedA
| is needed based on a configuration file.
|
| Dim lInstance As New cBase()
|
| This statement should then create an instance of ...
|
| Class cDerivedA
| Inherits cBase
| ...
| End Class
|
| During a different run the configuration file requires cDerivedB being
| used ...
|
| Dim lInstance As New cBase()
|
| This statement should now create an instance of ...
|
| Class cDerivedB
| Inherits cBase
| ...
| End Class
|
| So I was trying to tweak the Public Sub New() in cBase. How can I call
| the New() of the appropriate cDerivedX Class and create an instance of
| it that will be accessed through lInstance?
|
| Thanks a lot!
| Joe
|
 

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