Results 1 to 2 of 2

Thread: How to use enum values in VB

  1. #1
    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.

  2. #2
    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.

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,954,691.14895 seconds with 16 queries