"Sathyaish" <Sa*******@Yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
:
: What is a private constructor, and why would a class have one?
I was playing around once with a web site where I defined a "User"
object which formed the basic security enforcement for the project. I
wanted to ensure that a "User" could not be instantiated with out a
valid ID and password. In this case, I created the following (highly
simplified) class:
Public Class User
Private mUID As String
Private Sub New
End Sub
Public Sub New(UID As String, PWD As String)
If Not ValidateCredentials(UID, PWD) Then
Throw New Exception
Else
mUID = UID
End If
End Sub
End Class
In this way, a consumer of the User class was prevented from creating
an instance of the class without supplying User ID and Password data.
That is, the following was disallowed
Dim U As New User
Instead the consumer had to instantiate the class in this fashion:
Dim U As New User("ID", "PWD")
If the ID/PWD combination could not be validated, the class threw an
exception. Otherwise the class was instantiated and the consumer could
then use this instance to perform what ever task this user had
permission to do.
In order to allow for the creation of a new user with a given ID/PWD
combination, I added the following function to the User class above:
Public Shared Function NewUser(UID As String, _
PWD As String) As User
If Not IsUniqueID(UID) Then
Throw New Exception
Else
Dim U As New User
U.mUID = UID
DataBase.AddUser(UID, PWD)
Return U
End If
End Function
This function first of all ensured that the user ID was unique to the
project. If not, it threw an exception. Otherwise it create a new User
object, updated the mUID member field if that object, added this user to
the database, then returned the instance of the User class to the
caller.
Since this function is declared as "Shared" (static in C#), consumers of
the class didn't have to have an instance of the User class to call it.
This allowed for the following code (assume that user ID "RALF" did not
currently exist in the project):
Public Class MyClass
Public Shared Sub Main
Dim U As User
U = User.NewUser("RALF", "password")
End Sub
End Class
Note that U is not declared as a 'New User'. This is because the empty
constructor is private and would generate a compiler error. Note also
that 'Dim U As New User("RALF", "password")' is also not allowed since
"RALF" is not a valid user id value. Finally, note that the function
call to User.NewUser(... is not made from an instance of the User class
directly. It could be, but it doesn't have to be as this example shows.
If the function did not have the "Shared" keyword in its definition,
that function call would be illegal.
This highlights a few interesting features about classes. First of all,
while the private constructor was not accessible to an object not part
of the User class itself, it was accessible to the member function
NewUser. This allowed that function to create a new instance of the
class without having to supply the ID and password parameters.
Note also that once that a new instance was created by that function,
the function had full access to the new object's private member fields.
What that means is, any object of a given type can view and manipulate
the private (and otherwise inaccessible) member fields of any other
object of that same type. Of course, this will only happen if the class
is coded to do so (as in the example above).
: What are the other kinds of constructors besides:
:
: (1) public constructors; and
: (2) parameterized constructors
:
: And I understand that they are not mutually exclusive of one
: another. The above classification assimilates my knowledge of
: having used constructors in both the above manners.
Ralf