468,720 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

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 2653
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Asfand Yar Qazi | last post: by
4 posts views Thread by Jerry Krinock | last post: by
4 posts views Thread by Mike Labosh | last post: by
2 posts views Thread by Sathyaish | last post: by
31 posts views Thread by Peter E. Granger | last post: by
4 posts views Thread by Colin McGuire | last post: by
6 posts views Thread by daveb | last post: by
7 posts views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.