answersLogoWhite

0

What are enumerators?

User Avatar

Anonymous

15y ago
Updated: 9/17/2019

http://aspnet.4guysfromrolla.com/articles/042804-1.aspx Introduction An enumeration is a special type in the .NET Framework that is comprised of a number of named constants. While you might not have created an enumeration type yourself, you have likely used enumerations many times in day-to-day programming. For example, each item in an ASP.NET DataGrid has an ItemType property, which is of type ListItemType. ListItemType is an enumeration with values like Header, Item, AlternatingItem, Footer, and others. Too, if you've created files with the System.IO.File class you've used the FileMode and FileAccess enumerations; if you have worked with regular expressions, you've likely used the RegexOptions enumeration. When creating your own classes and components, enumerations can be used to make your code strongly typed, which makes the code more readable and less prone to errors. Creating and using enumerations is simple, so there's no reason not to use enumerations where applicable. This article looks at how to create enumeration types, how enumerations differ from constants, how to create enumerations whose members can be combined via bit-wise operations, and how to enumerate the members of an enumeration. Creating an Enumeration Type An enumeration can be declared with the following syntax: ' -- VB.NET Public Enum EnumName [As Type] Carriage-return delimited list of Named Constants End Enum // -- C# public enum EnumName [: Type] { Comma-delimited list of Named Constants } EnumName is the name of the enumeration type, Type is the underlying value of the enumeration, and Named Constants is a set of names and optional values corresponding to the Type. All enumerations have some core, underlying, integral value type, such as a Byte, Integer, or Long. The default underlying type is Integer. An example of creating an enumeration can be seen below: ' -- VB.NET Public Enum MenuItemType Header Item AlternatingItem End Enum // -- C# public enum MenuItemType { Header, Item, AlternatingItem } Enumerations vs. Constants A constant is single element whose value never changes, and is declared using the following syntax: ' -- VB.NET Public Const ConstantName As Type = Value // -- C# public const Type ConstantName = Value Where ConstantName is the constant's name, Type is the type, and Value is the constant's value, which can never be changed. The benefit of using constants is that they give a name to some "magic number," which improves code readability. Unfortunately, constants don't provide strong typing. For example, imagine we had the following constants defined: Public Const Header As Integer = 1 Public Const Item As Integer = 2 Public Const AlternatingItem As Integer = 3 Additionally, imagine that we have a method, CreateMenuItem(Integer), that takes in an Integer specifying the type of the menu item to create. This method might be called from the code like: 'Create a Header menu item... CreateMenuItem(Header) 'Create an AlternatingItem menu item... CreateMenuItem(AlternatingItem) While this approach is clearly more readable that calling CreateMenuItem(1) or CreateMenuItem(3), the method allows any Integer to be passed in. That is, a call to CreateMenuItem(-1934) wouldn't cause a compile-time error (although it would likely cause a runtime error of some sort). An enumeration, on the other hand, is strongly typed. Rather than using constants, we could use an enumeration. The CreateMenuItem() method, then, would expect an input parameter of type MenuItemType, and would be invoked using code like CreateMenuItem(MenuItemType.Header) or CreateMenuItem(MenuItemType.AlternatingItem). With this approach, a compile-time error would be noted if we attempted to call CreateMenuItem(-1934). Enumerations Under the Covers When compiling a project that includes enumerations, the enumeration is converted into a structure that turns each of the enumeration's named constants into an actual constant. That is, an enumeration like: public enum MenuItemType { Header, Item, AlternatingItem } Would get turned into the following at compile-time: public struct MenuItemType : System.Enum { public const int Header = 0; public const int Item = 1; public const int AlternatingItem = 2; } Notice that this structure is derived from the System.Enum class. The System.Enum class contains the base functionality for all enumerations and provides methods for accessing the individual items of an enumeration (as we'll see shortly). Creating Enumerations Whose Members Can Be Joined via Bit-Wise Operations What happens if your function or class needs to be passed more than one or more of your enumeration's values? For example, the RegexOptions class spells out a number of regular expression options: if the regular expression should be compiled, whether it should ignore case, whether the regular expression should have the ^ and $ symbols match on a single line, or on multiple lines, and so on. Since a regular expression might need to be configured to support more than one of these options, the bit-wise OR operator can be used. For example, to create a regular expression object that is both multiline and compiled, you'd use: ' -- VB.NET Dim re as New Regex(pattern, _ RegexOptions.MultiLine OR RegexOptions.Compiled) // -- C# Regex re = new Regex(pattern, RegexOptions.MultiLine | RegExOptions.Compiled); Notice that in VB.NET the bit-wise OR operator is OR; in C# it's |. By default, an enumeration's members cannot be bit-wise ORed together. In order to create an enumeration that supports bit-wise operations you need to do two things: Mark the enumeration with the Flags() attribute Assign the members of the enumerations values corresponding to the powers of 2 Here's a sample of how to declare an enumeration that can support bit-wise operations: ' -- VB.NET Public Enum Characteristics None = 1 Tall = 2 Dark = 4 Handsome = 8 All = Tall Or Dark Or Handsome End Enum // -- C# [Flags()] public enum Characteristics { None = 1, Tall = 2, Dark = 4, Handsome = 8, All = Tall | Dark | Handsome } Notice that each enumeration member has an increasing power of 2 value: 1, 2, 4, and 8. To compute the next value in the series, simply multiple the last value by 2. The reason we need the numbers to be powers of 2 is because the way integers are represented by a computer. A thorough discussion of this is beyond the scope of this article. Imagine we had a method called MakeIdealHusband(Characteristics). This method could be called like: MakeIdealHusband(Characteristics.Tall | Characteristics.Handsome), which would create a Tall and Handsome husband. Enumerating the Members of an Enumeration There may be times where you need to loop through an enumeration and display either the names within the enumeration or the values. The System.Enum class contains a GetNames(Type) method that returns a string array of the enumeration members. Here's an example of how to loop through your enumeration's names: 'Loop through the enumeration members Dim names As String() = System.Enum.GetNames(GetType(Characteristics)) For i as Integer = 0 To names.Length-1 Response.Write(names(i)) Next The System.Enum class also contains a Parse(Type, String) method that can be used to get a strongly typed enumeration instance back based on a specified enumeration name. For example, to get an enumeration instance of Characteristics.Tall, we could use the following code: 'Convert string to enumeration type Dim myMan As Characteristics = System.Enum.Parse(GetType(Characteristics), "Tall") Conclusion Enumerations are good for lists of things that never or very rarely change. They are not good for lists that have frequent modifications since you would have to recompile your code every time something in your enumeration changed. Your enumeration must have values with one of the following data types: Byte, Integer, Long, or Short. If you can "translate" the underlying value of your enumeration to one of these data types then that list is a good candidate for an enumeration. If your underlying values are Char or String data types, these lists are not good candidates for enumerations. Enumerations can make your coding a lot easier to read and understand by allowing you to refer to underlying lists of constants with logical names. Happy Programming! By Rachael Schoenbaum and Scott Mitchell

User Avatar

Wiki User

19y ago

What else can I help you with?

Related Questions

How many enumerators were employed for census work in 1951?

bhavugdwgdexdx


How many enumerators worked on the first census?

The first census in the United States, conducted in 1790, employed approximately 650 enumerators. These individuals were responsible for gathering population data across the newly formed states and territories. The census aimed to count every person living in the country, and the enumerators played a crucial role in ensuring the accuracy of the count.


Who are enumerators?

Enumerators are individuals who are responsible for collecting data through surveys or interviews. They are often employed by various organizations to gather information from respondents in a systematic manner. Enumerators play a crucial role in ensuring accurate data collection by following survey protocols and maintaining a professional and unbiased approach while interacting with participants.


How much is the pay for census enumerators in Kenya for the 24th and 25th August 2009 census?

The Kenyans Enumerators are going to be paid Ksh. 1500 * 7 days for the work they did!!Let us not anticipate getting paid Ksh. 24,000/= by the contracts signed for!!This is the big fish and small fish issue!!!


What actors and actresses appeared in The Enumerators - 2010?

The cast of The Enumerators - 2010 includes: Joe Backer as Diaz Anita Cordell as Conti Cameron Haines as Kellar Victor Hentzen as Griffin Fletcher Sean Hogge as Crazy Guy Chanel Jones as Greta Fletcher Austin Lee Jones as Water Gun Kid Jerry Wheeler as Bob Kruger Carolyn Wheeler as Carol Kruger


What are the five methods for recording data?

The five methods of recording data are: direct personal observation, local agents, mailed questionnaires, indirect oral interviews, and schedule via enumerators.


How much does working for the US Census Bureau pay?

Normally the US Census Bureau hires enumerators (census takers) that are out surveying people, crew leaders, and clerical staff. I know that for a Special Census conducted in 2008 Enumerators were paid $14.00 per hour, crew leaders $15.50 per hour, and clerical staff $10.50 per hour. Mileage and paid training were also included. Pay depends on region and median income. For example, in Charlotte, NC enumerators are paid 18.50 an hour. On Long Island, NY, they are paid 24.50. Mileage for any field position is always added to that, currently .50 per mile. Check with you local Census office for the correct pay scale


Discuss the problem of data collection in Nigeria?

Misconception of Statistics Unco-operative Attitude of Respondents Statistical Literacy and Education Improper Record Keeping Funding Ineffective Statistical Legislation Dedication by Enumerators


What does the term enum refer to in computer programming?

The term 'enum' is short for 'enumerated type'. An enumerated type is a data type containing a set of values called elements. They can also be called members or enumerators.


What is a the work of an enumerator?

An enumerator collects data by conducting surveys, interviews, or observations. They are responsible for accurately recording information and ensuring the confidentiality of the data collected. Enumerators play a crucial role in gathering information for research, statistics, or demographic analysis.


What do you call people who do census?

People who conduct a census are typically called census enumerators or census takers. They are responsible for collecting demographic data from households to provide accurate population statistics. In some contexts, they may also be referred to as surveyors or data collectors.


Is there a list of names of US veterans who fought in Word War I?

If you are wondering whether or not a particular individual served in the US army or navy during World War I, and if that person lived to 1930, check the 1930 US census. Enumerators asked whether or not individuals were veterans.