473,587 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1638
"Joe HM" <un*******@yaho o.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.Creat eInstance 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*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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
10340
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 virtual static member function in a base classe? If not, is there an better elegant solution for this problem? Thanks in advance, Anthony Lansbergen
9
4984
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 pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived...
11
19443
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 base class) then override(in the derived class), but fails to compare and contrast the two... I've read this (short snippet)three times and by...
4
4324
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 Methods?
6
6655
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
7
6606
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
11
2999
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. Is writing a long series of assignments in a Clone() statement my only option? I know that I can write a Clone method using MemberwiseClone(),...
9
1648
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
12
4797
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.