Procedura: dichiarare enumerazioni (Visual Basic)How to: Declare Enumerations (Visual Basic)
Si crea un'enumerazione con l'istruzione Enum
nella sezione delle dichiarazioni di una classe o di un modulo.You create an enumeration with the Enum
statement in the declarations section of a class or module. Non è possibile dichiarare un'enumerazione all'interno di un metodo.You cannot declare an enumeration within a method. Per specificare il livello di accesso appropriato, utilizzare Private
, Protected
, Friend
o Public
.To specify the appropriate level of access, use Private
, Protected
, Friend
, or Public
.
Un tipo di Enum
ha un nome, un tipo sottostante e un set di campi, ognuno dei quali rappresenta una costante.An Enum
type has a name, an underlying type, and a set of fields, each representing a constant. Il nome deve essere un qualificatore di Visual Basic .NET valido.The name must be a valid Visual Basic .NET qualifier. Il tipo sottostante deve essere uno dei tipi Integer, ovveroByte
, Short
, Long
o Integer
.The underlying type must be one of the integer types—Byte
, Short
, Long
or Integer
. il valore predefinito è Integer
.Integer
is the default. Le enumerazioni sono sempre fortemente tipizzate e non sono intercambiabili con i tipi di numeri interi.Enumerations are always strongly typed and are not interchangeable with integer number types.
Le enumerazioni non possono avere valori a virgola mobile.Enumerations cannot have floating-point values. Se a un'enumerazione viene assegnato un valore a virgola mobile con Option Strict On
, viene restituito un errore del compilatore.If an enumeration is assigned a floating-point value with Option Strict On
, a compiler error results. Se Option Strict
è Off
, il valore viene convertito automaticamente nel tipo di Enum
.If Option Strict
is Off
, the value is automatically converted to the Enum
type.
Per informazioni sui nomi e su come usare l'istruzione Imports
per rendere non necessaria la qualificazione del nome, vedere enumerazioni e qualificazione del nome.For information on names, and how to use the Imports
statement to make name qualification unnecessary, see Enumerations and Name Qualification.
Per dichiarare un'enumerazioneTo declare an enumeration
Scrivere una dichiarazione che includa un livello di accesso al codice, la parola chiave
Enum
e un nome valido, come negli esempi seguenti, ognuno dei quali dichiara unEnum
diverso.Write a declaration that includes a code access level, theEnum
keyword, and a valid name, as in the following examples, each of which declares a differentEnum
.Private Enum SampleEnum SampleMember End Enum Public Enum SampleEnum2 SampleMember End Enum Protected Enum SampleEnum3 SampleMember End Enum Friend Enum SampleEnum4 SampleMember End Enum Protected Friend Enum SampleEnum5 SampleMember End Enum
Definire le costanti nell'enumerazione.Define the constants in the enumeration. Per impostazione predefinita, la prima costante in un'enumerazione viene inizializzata su
0
e le costanti successive vengono inizializzate su un valore maggiore di quello della costante precedente.By default, the first constant in an enumeration is initialized to0
, and subsequent constants are initialized to a value of one more than the previous constant. Ad esempio, l'enumerazione seguente,Days
, contiene una costante denominataSunday
con il valore0
, una costante denominataMonday
con il valore1
, una costante denominataTuesday
con il valore di2
e così via.For example, the following enumeration,Days
, contains a constant namedSunday
with the value0
, a constant namedMonday
with the value1
, a constant namedTuesday
with the value of2
, and so on.Public Enum Days Sunday Monday Tuesday Wednesday Thursday Friday Saturday End Enum
È possibile assegnare in modo esplicito valori alle costanti in un'enumerazione usando un'istruzione di assegnazione.You can explicitly assign values to constants in an enumeration by using an assignment statement. È possibile assegnare qualsiasi valore intero, inclusi i numeri negativi.You can assign any integer value, including negative numbers. È ad esempio possibile che si desideri che le costanti con valori minori di zero rappresentino le condizioni di errore.For example, you may want constants with values less than zero to represent error conditions. Nell'enumerazione seguente alla costante
Invalid
viene assegnato in modo esplicito il valore–1
e alla costanteSunday
viene assegnato il valore0
.In the following enumeration, the constantInvalid
is explicitly assigned the value–1
, and the constantSunday
is assigned the value0
. Poiché si tratta della prima costante nell'enumerazione,Saturday
viene inizializzata anche sul valore0
.Because it is the first constant in the enumeration,Saturday
is also initialized to the value0
. Il valore diMonday
è1
(uno più del valore diSunday
); il valore diTuesday
è2
e così via.The value ofMonday
is1
(one more than the value ofSunday
); the value ofTuesday
is2
, and so on.Public Enum WorkDays Saturday Sunday = 0 Monday Tuesday Wednesday Thursday Friday Invalid = -1 End Enum
Per dichiarare un'enumerazione come un tipo esplicitoTo declare an enumeration as an explicit type
Specificare il tipo di enum usando la clausola
As
, come illustrato nell'esempio seguente.Specify the type of the enum by using theAs
clause, as shown in the following example.Public Enum MyEnum As Byte Zero One Two End Enum
Vedere ancheSee also
- Qualifica di nomi ed enumerazioniEnumerations and Name Qualification
- Procedura: Fare riferimento a un membro di enumerazioneHow to: Refer to an Enumeration Member
- Procedura: scorrere un'enumerazione in Visual BasicHow to: Iterate Through An Enumeration in Visual Basic
- Procedura: Determinare la stringa associata a un valore di enumerazioneHow to: Determine the String Associated with an Enumeration Value
- Quando usare un'enumerazioneWhen to Use an Enumeration
- Cenni preliminari sulle costantiConstants Overview
- Tipi di dati costanti e letteraliConstant and Literal Data Types
- Costanti ed enumerazioniConstants and Enumerations
Commenti
Caricamento dei commenti...