473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
4 1618
"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
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
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
Hello!

This is great! Thanks so much!

Joe

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Anthony Lansbergen | last post by:
Hi, I want to create an abstract base class for singletons. The base class should demand that the derived classes implement a static instance member operation. Is it possible to create a pure...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
4
by: Akhil | last post by:
Hi All, Can u please explain this. Base Obj = new Derived(); Can Obj access methods both of Base and Derived or what will be the behaviour? What will be the behaviour for Overridden...
6
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
11
by: K.K. | last post by:
Suppose I have a class called Vehicle with many fields. Now I make a new class derived from Vehicle called Car. I'd like to make a method to copy the data from a Vehicle instance to a Car instance....
9
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
12
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.