Enum Statement

   
Enum Statement

Syntax

   accessmodifier   Enum   name [As type]     membername   [=   constantexpression   ]   membername   [=   constantexpression   ]    ... End Enum 
accessmodifier (optional; Keyword)

The possible values of accessmodifier are Public , Private , Friend , Protected , or Protected Friend . For more information, see Section 4.7 in Chapter 4.

name (required; String literal)

The name of the enumerated data type.

membername (required; String literal)

The name of a member of the enumerated data type.

constantexpression (optional; Long)

The value to be assigned to membername .

type (optional; Keyword)

The data type of the enumeration. All enumerated members must be integers; possible values are Byte , Short , Integer , and Long .

Description

Defines an enumerated data type. All of the values of the data type are defined by the instances of membername .

Rules at a Glance

  • The Enum statement can only appear at module level, in the declarations section of a form, code module, or class module.

  • Access rules for Enums are the same as for variables and constants. In particular, the optional accessmodifier can be any one of the following: Public , Private , Protected , Friend , or Protected Friend . The following table describes the effects of the various access modifiers:

 

Direct access scope

Class/object access scope

Private

Declaring class

Declaring class

Protected

All derived classes

Declaring class

Friend

Derived in-project classes

Declaring project

Protected Friend

All derived classes

Declaring project

Public

All derived classes

All projects

  • constantexpression can be either a negative or a positive number. It can also be another member of an enumerated data type or an expression that includes integers and enumerated data types.

  • If you assign a floating point value to constantexpression , it is automatically rounded and converted to an integer only if Option Strict is off; otherwise , it generates a compiler error.

  • If you do not specify type , it defaults to Integer .

  • If constantexpression is omitted, the value assigned to membername is 0 if it is the first expression in the enumeration. Otherwise, its value is 1 greater than the value of the preceding membername .

  • The values assigned to membername cannot be modified at runtime.

Programming Tips and Gotchas

  • Once you define an enumerated type, you can use name as the return value of a function. For example, given the enumeration:

     Public Enum enQuarter    enQ1 = 1    enQ2 = 2    enQ3 = 3    enQ4 = 4 End Enum 

    you can use it as the return value of a function, as illustrated by the following function declaration:

     Public Function QuarterFromDate(datVar as Date) _                 As enQuarter 

    You can also use it in a procedure's parameter list when defining a parameter's data type, as in the following code fragment:

     Public Function GetQuarterlySales(intQ As enQuarter) _                 As Double 
  • Although you can declare an enumerated type as the argument to a procedure or the return value of a function, VB.NET does not provide type safety in these cases. That is, if the value of the argument or the return value of the function is outside of the range of the enumerated type, VB.NET does not generate an error. In cases such as these, you should rely on validation routines to make sure that an input value is in fact within the range of an enumerated type.

  • Individual values of an enumerated type can be used in your program just like normal constants, except that they must be prefaced with the name of the enumeration.

  • Enumerated types provide the advantage of allowing you to replace numeric values with more mnemonic labels and of allowing you to select values using the Auto List Members feature in the Visual Studio IDE.

  • If you want to retrieve or display the name of an enumerated member rather than its value, you can use the member's ToString method. For example:

     Public Module modMain Public Enum WorkDayTypes    Weekday = 0    Weekend = 1    Holiday = 2    Floating = 3    Personal = 4    Vacation = 5 End Enum Public Sub Main    Dim enDay As WorkDayTypes = WorkDayTypes.Vacation    Console.WriteLine(enDay.ToString(  ))   ' Displays                                           '"Vacation" End Sub End Module 

VB.NET/VB 6 Differences

  • In VB 6, members of an enumeration can be accessed without having to qualify them with the name of the enumeration to which they belong. In VB.NET, this behavior is not permitted; all members of an enumeration can only be accessed by referring to the name of their enumeration.

  • In VB 6, all enumerated members are Longs. In contrast, VB.NET allows you to define the integer data type of the enumeration's members.

  • In VB 6, members of a public enumeration can be hidden from the Object Browser by adding a leading underscore to the member name. For example, in the enumeration:

     Public Enum Primes    [_x0] = 0    x1 = 1    x2 = 3 End Enum 

    the constant _x0 is hidden in Intellisense and the Object Browser unless the Object Browser's Show Hidden Members option is selected. In Visual Studio .NET, a leading underscore does not hide a member.

See Also

Const Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net