471,570 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Custom values in Enum

Vi
Hi,
Is there a way to declare an Enum with non-secventual values? For example I
want to declare the following enum:
public enum Days :byte {Sat=1, Sun, Mon, Tue, Wed};

where Sat=1, Sun=2, Mon=12, Tue=13, Wed=14}

Is this possible?
Thanks

Jan 14 '06 #1
2 1599
Yes. See the language spec for more.
http://msdn.microsoft.com/library/de...pspec_14_3.asp

public enum Days
{
Sat = 1,
Sun = 2,
Mon = 12,
Tue = 13,
Wed = 14
}

--
Tim Wilson
..NET Compact Framework MVP

"Vi" <Vi@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
Hi,
Is there a way to declare an Enum with non-secventual values? For example I want to declare the following enum:
public enum Days :byte {Sat=1, Sun, Mon, Tue, Wed};

where Sat=1, Sun=2, Mon=12, Tue=13, Wed=14}

Is this possible?
Thanks

Jan 14 '06 #2
Of course, just assign each value explicitly:

public enum Days {Sat=1,Sun=2,Mon=12,Tue=13,Wed=14}

If I recall correctly the following will work too, as subsequent values
are sequential:

public enum Days {Sat=1,Sun,Mon=12,Tue,Wed}

This is one of those "check the docs" and/or "try it and see" kinds of
issues, you could answer the question in about 60 seconds that way.

Incidentally I would not take pains to make it a byte enum unless you
expect to store large amounts of the values in memory or something.
Generally, ints are handled more efficiently. The only reason to use
byte or short in or out of an enum is if you are going to hold a big
collection or matrix of them and you want to relieve memory pressure.
The system architecture is generally most efficient in terms of access
speed, with ints.

Best,

--Bob

Vi wrote:
Hi,
Is there a way to declare an Enum with non-secventual values? For example I
want to declare the following enum:
public enum Days :byte {Sat=1, Sun, Mon, Tue, Wed};

where Sat=1, Sun=2, Mon=12, Tue=13, Wed=14}

Is this possible?
Thanks

Jan 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Dennis C. Drumm | last post: by
5 posts views Thread by John Smith | last post: by
1 post views Thread by Ed Bick | last post: by
2 posts views Thread by Harry F. Harrison | last post: by
6 posts views Thread by Rick | last post: by
4 posts views Thread by Rex the Strange | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.