Private Constructor

Q

Q2004

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

Guest

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
 
M

Morten Wennevik

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
 
G

Guest

Hi

Check this out about the need for private constructors

http://msdn.microsoft.com/library/d.../en-us/csref/html/vclrfPrivateConstructors.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/default.asp?url=/library/en-us/csref/html/vclrfimplicit.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
 
G

Guest

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
 
M

Morten Wennevik

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

Q2004

Hi Morten,

Thanks for your help. How do I create the instance in advance ?

One last question. How do I make a comparison to work :

Car mycar = Car.Nissan;

if (mycar == Car.Nissan)
{
}

Regards,
Q2004
 
R

Ravichandran J.V.

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

Top