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

calculating number of elements in an enumerated type

Is there an easy way to return the number of elements in an enumerated type
please
thanks
Claire
Nov 16 '05 #1
5 5606
Hi Claire,

Enum.GetNames(typeof(YourEnum)); // for names of enum
Enum.GetValues(typeof(YourEnum)); // for values of enum

Cheers

Marcin
Is there an easy way to return the number of elements in an enumerated type
please
thanks
Claire

Nov 16 '05 #2
"Claire" <bl****@blahhhhh.com> wrote in message news:ej****************@TK2MSFTNGP09.phx.gbl...
Is there an easy way to return the number of elements in an enumerated type


Given the enum,

enum BirthdayInvitee
{
Linda = 1,
Dexter = 2,
Jennifer = 3,
Renee = 4,
Joe = 5
}

Then I can get the length of the number of Enum names for this type like this,

int invitationsReqd = Enum.GetNames( typeof( BirthdayInvitee)).Length;
Derek Harmon
Nov 16 '05 #3
Jon
You could try Enum.GetValues(typeof( [name of enumeration here] )).Length
"Claire" <bl****@blahhhhh.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
Is there an easy way to return the number of elements in an enumerated type please
thanks
Claire

Nov 16 '05 #4
On Mon, 6 Sep 2004 09:52:15 +0100, "Claire" <bl****@blahhhhh.com>
wrote:
Is there an easy way to return the number of elements in an enumerated type
please
thanks
Claire

There are several useful replies involving Enum.GetValues.

However, if your enumeration doesn't set explicit values but just
lets them default to 0, 1, 2, ... then I sometime just include a Last
member, e.g.,

enum cardSuits {clubs, spades, diamonds, hearts, Last}

then the number of "real" values in the enumeration reduces
to cardSuits.Last which can be cast to whatever type of
counting variable you wish, int, long, byte...

Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 16 '05 #5
thanks everyone for your help
Nov 16 '05 #6

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

Similar topics

0
by: Brandon | last post by:
Hello all, and thanks for taking a look at this. First off, the project I am working on is a server/client application that contains a shared assembly of common classes between the two...
2
by: Daniel Wilson | last post by:
I have an enumerated type like this: public __value enum MyType{ ABC = 1, DEF = 2, GHI = 3, JKL = 4 }; Later I have a for loop like this: for (ft = MyType::ABC; ft <= MyType::JKL; ft++){
2
by: postings | last post by:
Hi How do I convert from string to Enumerated type I defined? i.e. Dim pagemode As myenumtype = CType(mystringvalue,???? ). Can't seem to work this out at all! Cheers
1
by: davidmcb | last post by:
I have defined an enumerated type that I use in various places in my schema. I am in the process of creating a new complex type that contains an attribute that I want to restrict to NOT being a...
8
by: King | last post by:
Hi I have following MS Acess query Here is the query ID Name Prgm ID Client ID Date Start Time End Time Minutes C4 Trisha TIP DEK0703 7 /7 /2006...
0
by: jjoy | last post by:
After upgrading to 2.0/VS2005, one of the web services we consume no longer worked. It defines an enumerated type that is then used in several classes within the proxy code. The proxy code now...
11
by: David Mathog | last post by:
Is there a standard compliant method to access the number of elements in an enumerated type in C, either from within the preprocessor or the running program? The example below compiles and runs...
4
by: Rex the Strange | last post by:
Hello all, Can anyone please tell me why I can't do this (and what I can do to get the equivalent effect): In the code: public enum myenum value1 value2
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...

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.