Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework


Enumerated Types with Explicitly Assigned Ordinality



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə79/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   75   76   77   78   79   80   81   82   ...   294
DelphiLanguageGuide

Enumerated Types with Explicitly Assigned Ordinality
By default, the ordinalities of enumerated values start from 0 and follow the sequence in which their identifiers are
listed in the type declaration. You can override this by explicitly assigning ordinalities to some or all of the values in
the declaration. To assign an ordinality to a value, follow its identifier with = constantExpression, where
constantExpression is a constant expression that evaluates to an integer. For example,
type Size = (Small = 5, Medium = 10, Large = Small + Medium);
defines a type called 
Size
 whose possible values include 
Small

Medium
, and 
Large
, where 
Ord(Small)
 returns
5, 
Ord(Medium)
 returns 10, and 
Ord(Large)
 returns 15.
An enumerated type is, in effect, a subrange whose lowest and highest values correspond to the lowest and highest
ordinalities of the constants in the declaration. In the previous example, the 
Size
 type has 11 possible values whose
ordinalities range from 5 to 15. (Hence the type 
array[Size] of Char
 represents an array of 11 characters.)
Only three of these values have names, but the others are accessible through typecasts and through routines such
as 
Pred

Succ

Inc
, and 
Dec
. In the following example, "anonymous" values in the range of 
Size
 are assigned to
the variable 
X
.
66


var X: Size;
          
  X := Small;    // Ord(X) = 5
  Y := Size(6);  // Ord(X) = 6
  Inc(X);        // Ord(X) = 7
Any value that isn't explicitly assigned an ordinality has ordinality one greater than that of the previous value in the
list. If the first value isn't assigned an ordinality, its ordinality is 0. Hence, given the declaration
type SomeEnum = (e1, e2, e3 = 1);
SomeEnum
 has only two possible values: 
Ord(e1)
 returns 0, 
Ord(e2)
 returns 1, and 
Ord(e3)
 also returns 1;
because 
e2
 and 
e3
 have the same ordinality, they represent the same value.
Enumerated constants without a specific value have RTTI:
type SomeEnum = (e1, e2, e3);
whereas enumerated constants with a specific value, such as the following, do not have RTTI:
type SomeEnum = (e1 = 1, e2 = 2, e3 = 3);

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   75   76   77   78   79   80   81   82   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin