473,799 Members | 3,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a constructor from another

How does a constructor of one class call another of the same class?
When would this mechanism make sense or be required?
PS: Two instances I saw are:

(a) While using the Singleton pattern
(b) While using the Factory pattern

Nov 21 '05 #1
2 3220
Sathyaish,
To call a base constructor you use MyBase.New as the first statement of your
constructor. NOTE: If you do not explicitly call a base constructor, the
default base constructor, if available, will implicitly be called for you.

To call another constructor in the same class you use MyClass.New as the
first statement of your constructor.

For example:

Public MustInherit Class Base

Public Sub New(name As String)
' save name someplace
End Sub

End Class

Public Class Derived
Inherits Base

Public Sub New(name As String)
MyClass.New(nam e, "Some Default Value")
End Sub

Public Sub New(name As String, value As String)
MyBase.New(name )
' save value someplace
End Sub

End Class

Hope this helps
Jay
"Sathyaish" <Sa*******@Yaho o.com> wrote in message
news:11******** ************@g4 3g2000cwa.googl egroups.com...
| How does a constructor of one class call another of the same class?
| When would this mechanism make sense or be required?
|
|
| PS: Two instances I saw are:
|
| (a) While using the Singleton pattern
| (b) While using the Factory pattern
|
Nov 21 '05 #2
Sathyaish,

I don't know if I understand your question right, however when you are
asking about recursion, than what you ask is necessary when you are
accessing a tree with undefined nodes.

I hope this helps,

Cor
Nov 21 '05 #3

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

Similar topics

6
1905
by: Asfand Yar Qazi | last post by:
Hi, Consider this: class C { int j; public: C(int i) : j(i)
2
1921
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
4
7293
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in this demo, but the second time the constructor is called, its "this" points to a different location. ...
4
2239
by: Mike Labosh | last post by:
Greetings, all: I want to invoke one constructor from another to gain code reuse. In the snip below, I want my second constructor to pass its first argument to the first constructor so that the first constructor can set the Provider property and invoke the resetConnection() method first, before the second constructor assigns the connection string to the Connection property, but I can't seem to work out the syntax.
2
3840
by: Sathyaish | last post by:
How does a constructor of one class call another of the same class? When would this mechanism make sense or be required? PS: Two instances I saw are: (a) While using the Singleton pattern (b) While using the Factory pattern
31
5199
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I haven't been able to figure out is how to call one constructor from another within a class. It's easy enough to call the base class's constructor from the derived class, but that's not what I'm trying to do. For example, in Java (or J#) it's easy...
4
1150
by: Colin McGuire | last post by:
Hi, I have a class with lots of constructors - one is shown below Public Sub New(ByVal rows As Integer, ByVal columns As Integer) InitializeComponent() 'And code in here that sets instance variables, 'and does calculations based on rows and columns passed 'in in the constructor, and lots more. End Sub Another of my constructors has no parameters, and there are various
4
4974
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int iSourceSampleRate, int iTargetSampleRate ) {
6
2468
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines (the map constructor calls), saying that I'm trying to take the address of a constructor. Another compiler complains about all four of the last lines, just saying "invalid use of class". What is the correct syntax for calling a container...
7
2689
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10243
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9078
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
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
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.