Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-08-2009
Member
 
Join Date: Nov 2008
Posts: 1,221
How to use enum values in VB

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.
Reply With Quote
  #2  
Old 10-08-2009
Member
 
Join Date: Nov 2008
Posts: 1,221
Re: How to use enum values in VB

Note:

1. The Enum statement can appear only at module, namespace, or file level. This means you can declare enumerations in a source file or inside a module, class, or structure, but not inside a procedure. Once the Enum type is defined, it can be used to declare the type of variables, procedure arguments, and Function returns.

2. Enumerations can be accessed from anywhere within the module, class, or structure in which they are declared. An enumeration and all its members are Public by default. To specify the accessibility in more detail, include Public, Protected, Friend, Protected Friend, or Private in the Enum statement. The accessibility you specify applies to all the members as well as to the enumeration itself.

3. An enumeration is a related set of constants. The enumeration members between the Enum and End Enum statements are initialized to constant values.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use enum values in VB"
Thread Thread Starter Forum Replies Last Post
W32 registry values are not getting matched by the default values Angrzej Networking & Security 5 19-05-2011 12:23 PM
What is an ENUM in Networking? LajJlI Networking & Security 7 07-03-2011 03:29 AM
Java - question on enum ISAIAH Software Development 5 02-03-2010 10:50 AM
Problem in Enum HardWeaR Software Development 2 18-11-2009 02:38 PM
How to convert string to enum in vb.net application GunFighter Software Development 3 30-07-2009 01:12 PM


All times are GMT +5.5. The time now is 02:02 PM.