What is this concept? - Shared Sub New

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've inherited some legacy code that I'm supposed to base my own work on. I
thought I was a fairly experienced VB.Net programmer but I've never seen
anything like this before.

The programmer has created a NotInheritable class full of shared properties
and functions. It contains the following declarations:

Private Sub New()
'Hiding the new preventing from being created.
End Sub

Shared Sub New()
.... pile of what appears to be initialization code...
End Sub

What is the basic concept here and when (and how) does New get called?
 
The programmer has created a NotInheritable class full of shared properties
and functions. It contains the following declarations:

Private Sub New()
'Hiding the new preventing from being created.
End Sub

Shared Sub New()
... pile of what appears to be initialization code...
End Sub

What is the basic concept here and when (and how) does New get called?

The Shared constructor is used to initialize Shared members.

The Private constructor is there to prevent anyone (except the class
itself) to create an instance of the class. If all instance
constructors are private then external code can't instantiate the
class.

Another way to prevent instantiation is of course to make the class
MustInherit, but that doesn't always make sense. One common example is
when you implement the Singleton pattern.


Mattias
 
The Shared New gets called during the first access to the class (as I
understand, before you can access any field/method the class will be
initialized).
You'd better look at MSDN for static constructors description.

--
Best regards,
Vladimir

"B. Chernick" <[email protected]> Ñообщил/Ñообщила в
новоÑÑ‚ÑÑ… Ñледующее:
news:[email protected]...
 
Back
Top