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



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

Enumerated Types
An enumerated type defines an ordered set of values by simply listing identifiers that denote these values. The values
have no inherent meaning. To declare an enumerated type, use the syntax
typetypeName 
= (
 val1 
, ...,
 valn 
)
where typeName and each val are valid identifiers. For example, the declaration
type Suit = (Club, Diamond, Heart, Spade);
defines an enumerated type called 
Suit
 whose possible values are 
Club, 
 
Diamond

Heart
, and 
Spade
, where
Ord(Club)
 returns 0, 
Ord(Diamond)
 returns 1, and so forth.
When you declare an enumerated type, you are declaring each val to be a constant of type typeName. If the val
identifiers are used for another purpose within the same scope, naming conflicts occur. For example, suppose you
declare the type
type TSound = (Click, Clack, Clock)
Unfortunately, 
Click
 is also the name of a method defined for TControl and all of the objects in VCL that descend
from it. So if you're writing an application and you create an event handler like
procedure TForm1.DBGridEnter(Sender: TObject);
var Thing: TSound;
begin
   ...
   Thing := Click;
end;
you'll get a compilation error; the compiler interprets 
Click
 within the scope of the procedure as a reference to
TForm
's 
Click
 method. You can work around this by qualifying the identifier; thus, if 
TSound
 is declared in
MyUnit
, you would use
Thing := MyUnit.Click;
A better solution, however, is to choose constant names that are not likely to conflict with other identifiers. Examples:
65


type
  TSound = (tsClick, tsClack, tsClock);
  TMyColor = (mcRed, mcBlue, mcGreen, mcYellow, mcOrange);
  Answer = (ansYes, ansNo, ansMaybe)
You can use the (val1, ..., valn) construction directly in variable declarations, as if it were a type name:
var MyCard: (Club, Diamond, Heart, Spade);
But if you declare 
MyCard
 this way, you can't declare another variable within the same scope using these constant
identifiers. Thus
var Card1: (Club, Diamond, Heart, Spade);
var Card2: (Club, Diamond, Heart, Spade);
generates a compilation error. But
var Card1, Card2: (Club, Diamond, Heart, Spade);
compiles cleanly, as does
type Suit = (Club, Diamond, Heart, Spade);
var
  Card1: Suit;
  Card2: Suit;

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   74   75   76   77   78   79   80   81   ...   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