By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Joe HM" <un*******@yahoo.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #2

P: n/a
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

Nov 23 '05 #3

P: n/a
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
"Joe HM" <un*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
| 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
|
Nov 23 '05 #4

P: n/a
Hello!

This is great! Thanks so much!

Joe

Nov 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.