473,386 Members | 1,823 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,386 software developers and data experts.

enum in constructor

I want to use an enum field in a constructor.

For example:

class Student
{
private string name;
private enum university {.....};
private byte course;
......

public Student ( string name, enum university)
{
.........

}

I'm fairly new to programming so any sample code would be most appreciated.

}
Apr 11 '07 #1
4 9359
Hi Koljewa,

what do you mean by "enum field". A field is a variable (on class/struct
level).
You're defining an enum, what is a type, not a field. In this case a nested
type, because it is defined in another type.
To use this enum for a parameter in a public constructor, (or public
method), itself has to be public.
That's very usefull, because the caller has to know the type, to put a value
of that enum to the constructor.
Also you should consider not to define the enum as nested type. You also can
define enums at namespace level.

"??????" <@discussions.microsoft.comschrieb im Newsbeitrag
news:F4**********************************@microsof t.com...
>I want to use an enum field in a constructor.

For example:

class Student
{
private string name;
private enum university {.....};
private byte course;
......

public Student ( string name, enum university)
{
.........

}

I'm fairly new to programming so any sample code would be most
appreciated.

}

Apr 11 '07 #2
??????,

This won't work. The enumeration is defined as private, and has no
scope outside of the class that it is defined in. Because the constructor
is public, you can't expose a private type to the class as a parameter.
Instead, you have to declare the enumeration as public, like so:

class Student
{
private string name;
public enum university {.....};

Also, you might want to consider declaring your enumeration outside of
your class. It's more of a design issue. The decision to do so would
depend on what the enumeration represents. If the enumeration is specific
to that class, then its fine where it is, but if it is really something that
is not specific to the class, you should define it outside of the class.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"??????" <@discussions.microsoft.comwrote in message
news:F4**********************************@microsof t.com...
>I want to use an enum field in a constructor.

For example:

class Student
{
private string name;
private enum university {.....};
private byte course;
......

public Student ( string name, enum university)
{
.........

}

I'm fairly new to programming so any sample code would be most
appreciated.

}

Apr 11 '07 #3
Колева wrote:
I want to use an enum field in a constructor.

For example:

class Student
{
private string name;
private enum university {.....};
private byte course;
......

public Student ( string name, enum university)
{
.........

}

I'm fairly new to programming so any sample code would be most appreciated.
You probably want something like this:

enum University {
....
}

class Student
{
private string name;
private University uni;
private byte course;
...
public Student(string name, University uni){
...
}
}

Alun Harford
Apr 11 '07 #4
Thank you very much! :)

"Alun Harford" wrote:
Колева wrote:
I want to use an enum field in a constructor.

For example:

class Student
{
private string name;
private enum university {.....};
private byte course;
......

public Student ( string name, enum university)
{
.........

}

I'm fairly new to programming so any sample code would be most appreciated.

You probably want something like this:

enum University {
....
}

class Student
{
private string name;
private University uni;
private byte course;
...
public Student(string name, University uni){
...
}
}

Alun Harford
Apr 12 '07 #5

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

Similar topics

2
by: Eric C | last post by:
I'm reading a c++ book by Eric Nagler named Learning C++ and I ran into an exercise that requires a cpp file that defines several member functions of a header. Here is my problem, it defines a...
1
by: Eric C | last post by:
I'm reading a c++ book by Eric Nagler named Learning C++ and I ran into an exercise that requires a cpp file that defines several member functions of a header. Here is my problem, it defines a...
0
by: Patrick Guio | last post by:
Dear all, I wonder whether anyone might have a better idea/solution to the following. I need an associative container <int,string> for a limited and defined number of values (enum-like) but ir...
3
by: Teddy | last post by:
what is the special meaning of enum members in class ?
10
by: Steve | last post by:
Hi, I thought I had invented a pretty neat solution for my problem, but it turns out I'm using the named constructor idiom. :-) I've seen examples where empty structs are used to identify the...
3
by: Sanjay Pais | last post by:
I know that string/char enum is not possible in c# (.NET2.0) I need to create the equivalent of this: public enum HOW_GOOD { AWESOME = "A", GREAT= "G", NOT_TOO_BAD = "N", TERRIBLE="T" }
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
3
by: Gangreen | last post by:
I have the following enum. ** * An enum to represent types of mechanics. * * @author * @version 1.0 */ public enum Mechanics { ISOLATION(0),
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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,...

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.