By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Types of constructors

P: n/a
What is a private constructor, and why would a class have one? 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.

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Private Constructors - The only time I have used them is when I am creating
a class that holds shared members (non-instance).

Public Constructors - are what get used when you create an instance of a
class using the New keyword. This is the code where you init any internal
vars upon startup.

Parameterized Constructors - are used when you want to create an instance of
a class and initialize it with some values upon creation.
"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? 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.

Nov 21 '05 #2

P: n/a
Private constructors are normally used where the class is not meant to be
instantiated such as in class factories or singleton patterns.

HTH
"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? 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.

Nov 21 '05 #3

P: n/a
Sathyaish wrote:
What is a private constructor, and why would a class have one?
A private constructor is a constructor that can only be used from within the
class itself.

This normally makes the class fairly useless as nothing can actually create
instances of it, but it can be used alongside shared class members to make
sure nothing can instantiate the class but still allow the shared class
members to be used.

You could have a class that has one or more private constructors and also
one or more public constructors. This could allow you to pass internal
values to new instances of the class when it is instantiated by other code
residing within the class (by using a private constructor), but still allow
code outside of the class to create further instances (using a public
constructor).
What are the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors
You can also use Friend constructors. These may be called from anywhere with
your assembly, but cannot be seen or called from outside of the assembly.
These can be useful when you have a single object that creates and returns
instances of other objects from within the same assembly.
And I understand that they are not mutually exclusive of one another.


Indeed -- Public, Private and Friend describe the scope of the constructor,
that is, which other sections of code are able to see the constructor.
Parameterised constructors allow values to be passed into the constructors,
regardless of what the constructor's scope is.

I hope that all makes sense,

--

(O) e n o n e
Nov 21 '05 #4

P: n/a

"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
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.