Best Answer 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

โˆ™ 2006-02-26 21:07:54
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are enumerations?
Write your answer...
Related questions

What are enumerations in C?

Enumerations are groups of named constants.

What are standardised social enumerations?

Standardized social enumerations include provision of military and establishments of bankruptcy codes. It also includes collection of taxes and regulated trade.

'explain specification and implementation of enumerations with example in principle of programming languages by ravi sethi?

karika sethi karika sethi

What is mean by enumeration?

An enumeration in programming is a list of literals, grouped together for a common purpose. Members of an enumeration typically describe similar things by providing an alphanumeric mnemonic as a representative of a numerical identifier.For example, a Colors enumeration might be defined to contain the three member names Red, Blue and Green, representing numeric values 1, 2, and 3 (or any other value), respectively.In traditional languages such as C, enumerations are a convenience tool to improve readability of code. In C and similar languages, enumerations are not strongly typed, and allow assignment to and from integer values or other enumerations. In more modern languages, enumerations are strongly typed, so that Red from the Colors enumeration shown above cannot be confused with or assigned to Red from the Meatsenumeration.

What are the differences between enums and set of defines in c?

At the present time, there is little difference. The C Standard states that enumerations are compatible with integral types.

What are enumerations in c and c?

Enumeration type allows programmer to define their own data type . Keyword enum is used as a keyword for the enumerated data types.

What is a module in visual basic?

A Module is a file that stores many things such as subroutines, function, enumerations and other stuff so that you can use it in your application in a more organized way.

What is the significance of RBC enumeration?

low rbcs count shows anaemia high rbcs count show polycythemia vera. rbcs enumerations shows the bonemarrow how much active.

What are the element of the visual basic?

A declared element is a programming element that is defined in a declaration statement. Declared elementsinclude variables, constants, enumerations, classes, structures, interfaces, procedures, procedure parameters, function returns, external procedure references, operators, properties, events, and delegates

What are the data types in C and example?

Basic built-in data types in the C programming language are scalar types in variations of char, int and float:char, unsigned char, signed charint, signed int, unsigned int, short, signed and unsigned short, long, signed and unsigned longfloat, doubleNamed enumerations are supported. These are weakly typed constant literals of type int. Bitfields are supported as range-limited signed or unsigned integers. Neither qualify as a type in their own right.Some dialects also support additional types, such as wide character, extra large integers or floating point scalars.The language also knows a related keyword void, which is used to express the fact that the related item has no known or no specific type. void in itself is not a type (it is rather the absence of a type).The C programming language also supports three basic aggregates: structure, union and arrays.Pointers can reference any of the scalar built-ins, enumerations, structures, unions, arrays, and the infamous void. Pointers are viewed as a data type by many.Other members of the C family of languages also support class and interface types and strongly typed enumerations.

What are the uses of Packages in Java?

A package is a grouping of related types providing access protection and name space management. Here the term types may refer to classes, interfaces, enumerations, and annotation types. You can enclose all these into a package for easier identification.For ex: the package provides us with all java classes that can be used for input output operations.

What is the need of package in java?

A package is a grouping of related types providing access protection and name space management. Here the term types may refer to classes, interfaces, enumerations, and annotation types. You can enclose all these into a package for easier identification.For ex: the package provides us with all java classes that can be used for input output operations.

Examples of primitive data types in turbo c?

The primitive data types in C include:[signed|unsigned] char[signed|unsigned] short[signed|unsigned] int[signed|unsigned] long[signed|unsigned] long longfloatdoublelong doubleEnumerations (enum) and arrays may also be considered to be primitive types.

Which characters cannot be used in user-defined names in C?

The space and punctuation characters cannot be used in user-defined names. Only letters, digits and the underscore character are permitted, but a user-defined name cannot begin with a digit. User-defined names include function names, type definitions, enumerations, variables and constants.

Write a simple program in C plus plus to investigate the safety of its enumeration types?

If you want safety in enums, use an enum class rather than an ordinary enum. Ordinary enums can be implicitly converted to their underlying type whereas enum classes cannot. Enum classes are also scoped to their class, which avoids name-clashes when two enumerations have the same member.

What is the difference between java2 and j2ee?

Java2 is the 2nd incarnation of the Java language. There is J2SE(Java 2 Standard Edition) and J2EE(Java 2 Enterprise Edition). When one says Java2, typically J2SE is assumed.J2EE is just a subset of objects(classes, methods, enumerations, etc.) that relate to Enterprise applications. That includes database(JDBC), Servlets/JSP(web application programming and design), among a few other third party libraries for web services, networking protocols, etc.

What are the ways to write a cleaner code?

Never use a comment to express a concept that can be expressed in code; compilers don't read comments and users may ignore them. Avoid constant integers and "plain" enumerations; prefer enumeration classes. Avoid "naked" pointers; use resource handles and smart pointers. Avoid low-level coding unless absolutely necessary; lift the level of abstraction such that code becomes self-documenting. Generalise code to avoid code duplication, but do so without adding overhead.

What does enumeration in c-language mean?

Enumerated values/variables are used when a variable cannot be expressed in a native way, i.e. the variable is not intrinsically int, float, double, etc.For example, the day of the week really is the day of the week, not an int. A color is a color, not an int. So, you would express these are enums (enumerated) values:enum { red, green, blue} ;Using enumerations in this context also helps support developers manage changes to the code. enums are one of the mechanisms to replace "magic numbers" in code:if (color == 1 /*red*/){// do something}can be changed to:if (color == red){// do something}

What are the different data type in c programming?

Fundamental types in C:Boolean type (bool)Character types (char)Integer types (int)Floating-point types (float, double)A type to signify the absence of information (void)From these we can construct other types:Pointer types (such as int*)Array types (such as char[])In addition, a user can define additional types:Data structuresEnumerationsThe boolean, character and integer types are collectively known as the integral types.The integral and floating-point types are collectively known as the arithmetic types.The fundamental types and pointers are collectively known as the built-in types.Enumerations and structures are collectively known as user-defined types.Arrays can be constructed from user-defined types as well as built-in types.The short, long, signed and unsigned types are actually modifiers rather than types. However, if a type is not specified, the int type is assumed (a long is really a long int). The signed and unsigned modifiers can also be applied to char types. The long modifier can also be applied to the double type.All other built-in types (such as wchar_t, size_t, ptrdiff_t, and so on) are merely aliases for existing built-in types. For instance, size_t is typically defined as being an alias for unsigned short int.

What is common language specification in dotnet?

Common Language Specification (CLS) CLS is a set of rules provided to: describe the minimal and complete set of features to produce code that can be hosted by CLR ensure that products of compilers will work properly in .NET environment Sample rules: representation of text strings internal representation of enumerations definition of static members and This is a subset of the CTS which all .NET languages are expected to support. It was always a dream of Microsoft to unite all different languages in to one umbrella and CLS is one step towards that. Microsoft has defined CLS which are nothing but guidelines that language to follow so that it can communicate with other .NET languages in a seamless manner.

A company employee has very important data on a computer What RAID level should be used if the criteria include that data be written and read simultaneously and that all data be duplicated using a mi?

Mirroring is the method it is used. This is commonly in the specifications of RAID1, RAID5, RAID10, RAID1+0, RAID15, and others. RAID is commonly used to designate RAID (Redundant Array of Independant Disks) setups, but RAID is not technically recognized as a specification, and not all RAID types and enumerations (such as 15) are even recognized as valid designations. RAID may also not involve data redundancy or mirroring at all, such as in Striping (RAID0, RAID2, RAID3, RAID1+0, etc.)

Enumeration in c plus plus?

Enumerations are a method of grouping constant values. For example: enum suits { clubs, diamonds, spades, hearts }; By default, the first constant is assigned the value 0 and all subsequent values increment by 1. However, you can assign any value to any constant -- the automatic increments will continue from that point. enum suits { clubs = 1, diamonds, spades, hearts }; You can also assign the same value to multiple constants. enum suits { clubs = 1, diamonds, spades = 1, hearts }; By grouping constants within enumerations your code becomes more secure because you cannot pass constant literals into functions that expect an enumeration. Consider the following: void print_suit(unsigned id) { switch (id) { case (0): std:cout << "Clubs"; break; case (1): std:cout << "Diamonds"; break; case (2): std:cout << "Spades"; break; case (3): std:cout << "Hearts"; break; default: std::cout << "Invalid"; } } In the above example there is nothing to prevent the caller from passing an invalid value, such as 42, which the function caters for with a default case. However, by passing an enum instead, invalid values are completely eliminated: void print_suit(suits suit) { switch (suit) { case (clubs): std:cout << "Clubs"; break; case (diamonds): std:cout << "Diamonds"; break; case (spades): std:cout << "Spades"; break; case (hearts): std:cout << "Hearts"; } }

Descartes had four basic rules he used to arrive at the truth?

The following quotation from Discourse on Method presents the four precepts that characterize the Method itself:The first was never to accept anything for true which I did not clearly know to be such; that is to say, carefully to avoid precipitancy and prejudice, and to comprise nothing more in my judgment than what was presented to my mind so clearly and distinctly as to exclude all ground of doubt.The second, to divide each of the difficulties under examination into as many parts as possible, and as might be necessary for its adequate solution.The third, to conduct my thoughts in such order that, by commencing with objects the simplest and easiest to know, I might ascend by little and little, and, as it were, step by step, to the knowledge of the more complex; assigning in thought a certain order even to those objects which in their own nature do not stand in a relation of antecedence and sequence.And the last, in every case to make enumerations so complete, and reviews so general, that I might be assured that nothing was omitted. By clear and distinct he suggests the evidence of the senses. The enumerations have in time developed into many forms. He suggested drawing boxes on a paper, and connecting them. This idea has led to a multitude of graphic thinking aids that we use today.References^ Najm, Sami M. (July-October 1966). "The Place and Function of Doubt in the Philosophies of Descartes and Al-Ghazali". Philosophy East and West(Philosophy East and West, Vol. 16, No. 3/4) 16 (3-4): 133-41. doi:10.2307/1397536^ Descartes, Rene; Laurence J. Lafleur (trans.) (1960). Discourse on Method and Meditations. New York: The Liberal Arts Press. ISBN 0-672-60278-4.

Has Congress lived up to the expectations of the founding fathers?

I don't think so. The founders expected that the congress would be the dominant branch of government, formulating national policy. That was how it was for a long time. But today the congress just follows the lead of the president. They take his ideas and modify them, flesh them out with needed details, sometimes reject them altogether. But it is very rare these days that the congress initiates any major legislation itself. Michael Montagne The founding fathers would be appalled at what Congress has become. Check out Article 1, Section 8 of the Constitution. It specifically states the duties and powers of the legislative branch (and there aren't that many). Congress has since run roughshod over these enumerations and restrictions. Going by today's standards, you would think the document said that Congress has the power to legislate anything, and tax everything.

What are the different types of data types used in

In C#, data types can be categorised asValue Types.Reference Types.Value TypesVariables defined from Value Types store values. Copying one value type caribale tp another, doesn't affect the priginal vraible.They can be further categorised into: -1) structs: - They can be numeric (int, float, decimal), bool, user-defined structs.2) Enumerations: - They consist of a set of named constants. By default the first enumerator has value=0.Reference TypesThese objects store references to the actual data. These can be categorised into: -1) Classes: -- They encapsulate data and its functionality into a single entity called OBJECT.2) Interfaces: - These are used to declare the signatures, blurprints of methods, delegates and events.3) Delegates: - These contain the addresses/references to a method.For more information refer to related links.