220 likes | 931 Views
Enumeration. Definition. An enumeration is a set of related constants that define a value type where each constant is known as a member of the enumeration.
E N D
Definition • An enumeration is a set of related constants that define a value type where each constant is known as a member of the enumeration. • The enumerations provided by the .NET Framework are generally used to set object properties and to specify the values that are passed to methods.
What is Enum • An Enum is a value type with a set of related named constants often referred to as an enumerator list. • The Enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.
Enum • Enums type can be Integer (Decimal, Short, Byte, Double etc.). • To use one of the integer data types, you can code an As clause that specifies the data type after the enumeration name. • Enum is used to create numeric constants in .NET framework. All member of Enum are of Enum type.
Syntax of Enumeration Enum userdefinedname member name 1[=value] member name 2[=value]End Enum
Example of Enumeration Enum DayOfWeek Sun Mon Tue Wed Thu Fri Sat End Enum
Enumeration • An enumeration defines a set of related constants. Each constant is known as a member of the enumeration. • To specify other values for the constants, you can code an equal sign after the constant name followed by the integer value.
Example program of Enumeration Module Module1 Enum DayOfWeek Sun Mon Tue Wed Thu Fri Sat End Enum Sub Main()
Example Cont., Console.WriteLine("Day of week " & CInt(DayOfWeek.Sun) & " is " & DayOfWeek.Sun.ToString()) Console.WriteLine("Day of week " & CInt(DayOfWeek.Mon) & " is " & DayOfWeek.Mon.ToString()) Console.WriteLine("Day of week " & CInt(DayOfWeek.Tue) & " is " & DayOfWeek.Tue.ToString()) Console.WriteLine("Day of week " & CInt(DayOfWeek.Thu) & " is " & DayOfWeek.Wed.ToString()) Console.WriteLine("Day of week " & CInt(DayOfWeek.Fri) & " is " & DayOfWeek.Thu.ToString()) Console.WriteLine("Day of week " & CInt(DayOfWeek.Sat) & " is " & DayOfWeek.Fri.ToString()) Console.ReadLine() End SubEnd Module
Some points about Enum • Enums are enumerated data type in VB.NET. • Enums are not for end-user, they are meant for developers. • Enums are strongly typed constant. They are strongly typed, i.e. an Enum of one type may not be implicitly assigned to an Enum of another type even though the underlying value of their members are the same. • Enumerations (Enums) make your code much more readable and understandable.
Cont., • Enum values are fixed. • Enum can be displayed as a string and processed as an Integer. • The default type is Integer, and the approved types are Byte, Sbyte, Short, UShort, UInteger, Long, and ULong.
Cont., • Every Enum type automatically derives from System. • Enum and thus we can use System.Enum methods on Enums. • Enums are value types and are created on the stack and not on the heap. • The default and only access modifier supported is public.
The End …… Thank You ……