473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Private Constructor

What's the significant of private construtor ?

public class Car{
private string m_strName;
private string m_strType;
private int m_nID;

private Car(string name, string type, int ID)
{
m_strName = name;
m_strType = type;
m_nID = ID;
}

public static string Name
{
get
{
return m_strName;
}
}
}

I intended to create an enumeration class similar to Color
class in .NET framework.

Color mycolor = Color.Blue;

How should I modify the class above to make the following
works ?

Car car = car.NISSAN;

Thanks in advance.

Nov 15 '05 #1
6 1610
Hi,
What's the significant of private construtor ?

public class Car{
private string m_strName;
private string m_strType;
private int m_nID;

private Car(string name, string type, int ID)
{
m_strName = name;
m_strType = type;
m_nID = ID;
}

public static string Name
{
get
{
return m_strName;
}
}
}

I intended to create an enumeration class similar to Color
class in .NET framework.

Color mycolor = Color.Blue;

How should I modify the class above to make the following
works ?

Car car = car.NISSAN;
???
I hope you mind:

Car car = Car.NISSAN;

Add this sample to code of your class, to accomplish your needs:

public readonly static NISSAN = new Car("Nissan"
, "small"
, 1);

Regards

Marcin
Nov 15 '05 #2
A private constructor will prevent anyone (outside the class) from
instantiating that class

Car c = new Car(name, type, ID); // won't work outside the class
Car c = new Car(); // won't exist, since you have another constructor

if you add

public static Car Nissan
{
get
{
return new Car("Nissan", type, ID);
}
}

to get Car c = Car.Nissan; to work;

Happy coding!
Morten

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
> Add this sample to code of your class, to accomplish your needs:

my fault :)

public readonly static Car NISSAN = new Car("Nissan"
, "small"
, 1);

Marcin
Nov 15 '05 #4
Hi

Check this out about the need for private constructors

http://msdn.microsoft.com/library/de...onstructors.as

For your next question, how to do instantiation like the one below

Car car = car.NISSAN

I guess you can do this using Implicit operator overloading. check the below link for the same

http://msdn.microsoft.com/library/de...lrfimplicit.as

Hope this helps..

Regards
Madh

MVP | MCSD.NE

----- Q2004 wrote: ----

What's the significant of private construtor

public class Car
private string m_strName
private string m_strType
private int m_nID

private Car(string name, string type, int ID

m_strName = name
m_strType = type
m_nID = ID
public static string Nam

get

return m_strName;


I intended to create an enumeration class similar to Color
class in .NET framework.

Color mycolor = Color.Blue

How should I modify the class above to make the following
works

Car car = car.NISSAN

Thanks in advance


Nov 15 '05 #5
Actually, you wouldn't want a new car for every time you call Car.Nissan,
so you might want to create them in advance and just return the reference
for Nissan. This is how the Color class works, by returning a Color when
calling the Blue/Green/Salmon... properties of the class. Each color is a
separate property.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #6
Tan
May thanks for your helps.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op**************@msnews.microsoft.com...
Actually, you wouldn't want a new car for every time you call Car.Nissan,
so you might want to create them in advance and just return the reference
for Nissan. This is how the Color class works, by returning a Color when
calling the Blue/Green/Salmon... properties of the class. Each color is a
separate property.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
2
by: Andrew Robinson | last post by:
I need to create a shared static field for use within a number of different classes. Which one should I be using or are they all really the same thing? public class Widget { private Widget() {}...
1
by: Nick Wilton | last post by:
Hi guys, Hope someone here can help. I am performing something almost identical to the sample shown here, except my .NET server class has a private constructor and a shared factory method to...
1
by: Michael Carr | last post by:
Does anybody know how to inherit from a class with a private constructor? I am trying to inherit from DataRow as follows: public class MyDataRow : DataRow { private MyDataRow() { } }
4
by: plmanikandan | last post by:
Is static constructor available in C++? Can anybody explain me about private and static constructors in c++ Rgds, Mani
5
by: rajeev kumar singh | last post by:
what is the use of private constructor?
15
by: cmrhema | last post by:
Hello I have created a class also a constructor which is (should be, one of the conditions) private. Now I am supposed to call it in the main program. My team leader says there is a trick to call...
4
by: Rob Blackmore | last post by:
Hi, Can anyone advise how to call a private constructor using reflection? I currently get the above error which is rectified by changing the New() to Public from Friend but I ideally wish to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.