473,503 Members | 3,308 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 3196
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(name, "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*******@Yahoo.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.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
1881
by: Asfand Yar Qazi | last post by:
Hi, Consider this: class C { int j; public: C(int i) : j(i)
2
1897
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...
4
7256
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...
4
2222
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...
2
3827
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...
31
5155
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...
4
1120
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...
4
4941
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...
6
2441
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...
7
2663
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...
0
7064
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
7261
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,...
0
7315
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
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
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,...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
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 ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
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...

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.