Calling a constructor from another

  • Thread starter Thread starter Sathyaish
  • Start date Start date
S

Sathyaish

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
 
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
| 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
|
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top