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 = ( vall , ..., 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;


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:


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 Cardi: (Club, Diamond, Heart, Spade); var Card2: (Club, Diamond, Heart, Spade);

generates a compilation error. But var Cardi, Card2: (Club, Diamond, Heart, Spade);

compiles cleanly, as does type suit = (Club, Diamond, Heart, spade); var

Card1: suit; Card2: suit;

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment