Quick start manual

5-8
Delphi Language Guide
Simple types
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. (See
“Constant expressions” on page 5-44) 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.
var X: Size;
X := Small; // Ord(X) = 5
X := 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.
Subrange types
A subrange type represents a subset of the values in another ordinal type (called the
base type). Any construction of the form Low..High, where Low and High are constant
expressions of the same ordinal type and Low is less than High, identifies a subrange
type that includes all values between Low and High. For example, if you declare the
enumerated type
type TColors = (Red, Blue, Green, Yellow, Orange, Purple, White, Black);
you can then define a subrange type like
type TMyColors = Green..White;
Here TMyColors includes the values Green, Yellow, Orange, Purple, and White.