A common mistake made by most programmers is the use of hard coded literal strings or numeric values. To solve this problem, enum is used. Enumeration is a related set of constants. They are used when working with many constants of the same type. It's declared with the Enum keyword. It used at module, class, or structure level to declare an enumeration and define the values of its members.
Syntax
Code:
[ <attrlist> ] [{ Public | Protected | Friend | Protected Friend |
Private }] [ Shadows ] Enum name [ As type ]
[<attrlist1>] membername1 [ = initexpr1 ]
[<attrlist2>] membername2 [ = initexpr2 ]
...
[<attrlistn>] membernamen [ = initexprn ]
End Enum
Parameter
attrlist: Optional. List of attributes that apply to the enumeration or to this member. Multiple attributes are separated by commas.
Public: Optional. Enumerations declared with the Public keyword have public access. There are no restrictions on the accessibility of public enumerations.
Protected: Optional. Enumerations declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend: Optional. Enumerations declared with the Friend keyword have friend access. They are accessible from within their declaration context and from anywhere else in the same program.
Protected Friend: Optional. Enumerations declared with the Protected Friend keywords have the union of protected and friend access. They can be used by code in the same program, as well as by code in derived classes. Protected friend access can be specified only on members of classes.
Private: Optional. Enumerations declared with the Private keyword have private access. They are accessible only from within their declaration context, including from members of any nested types such as procedures.
Shadows: Optional. Indicates that this enumeration shadows an identically named programming element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable from within the derived class that shadows it, unless the shadowing element is inaccessible, for example if it is Private.
name: Required. Name of the enumeration. Must be a valid Visual Basic identifier. When you subsequently declare variables or arguments to be of this Enum type, you specify name for their data type.
type: Optional, even if Option Strict is On. Data type of the enumeration and all of its members. Can be Byte, Integer, Long, or Short. If you do not specify type, it defaults to Integer.
membername: Required. Name of this member of the enumeration. Must be a valid Visual Basic identifier. The accessibility of every member is Public, and you cannot declare it otherwise. However, if the enumeration itself is declared with Protected, Friend, Protected Friend, or Private access, this limits the accessibility of the members.
initexpr: Optional. Expression that is evaluated and assigned to this member when the enumeration is created. Can consist of a literal; a constant that is already defined; another enumeration member; or any combination of literals, constants, and enumeration members. You can use arithmetic and logical operators to combine such elements.
You cannot use variables or functions in initexpr. However, you can use conversion keywords such as CByte and CShort. You can also use AscW if you call it with a constant String or Char argument, since that can be evaluated at compile time.
If you do not specify initexpr, the value assigned is either zero (if it is the first membname), or greater by one than the value of the immediately preceding membname.
End Enum: Terminates an Enum block.
attrlist Parameters
attrname: Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs: Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit: Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.
Bookmarks