ENUMERATIONSIn this column, we have a detailed look at ENUMERATIONS in Delphi. An enumeration is a fixed range of named values. In other words, an enumaration is an ordinal type. Example: type TFruits = (frApple, frPear, frCherry, frBanana); where frApple stands for 0, frPear stands for 1, frCherry stands for 2 and frBanana stands for 3.> An enumerated variable contains one value out of a list that was defined previously in the corresponding type declaration. type In the Object Inspector, you use the property "Position" to set the size and placement of a form. Note: the Boolean type is also an enumeration, with two possible values: False and True.
Enumerated TypesAn enumerated type is just a shorthand way of assigning sequential values to constants. An enumerated type defines an ordered set of values. The values themselves have no inherent meaning,
they act only as "labels". type TypeName = (Val1, Val2, ..., Valn); where each Val is a constant of type TypeName. For example, the declaration: type TDirection = (dirNorth, dirEast, dirSouth, dirWest); defines an enumerated type called TDirection, whose possible values are dirNorth, dirEast, dirSouth, dirWest; and where Ord(dirNorth) returns 0, Ord(dirEast) returns 1, and so on. Subrange TypesA subrange type represents a subset of the values in another ordinal type. type TFruits = (frApple, frPear, frCherry, frBanana); you can then define a subrange type like: type TMyFruits = frApple..frPear; Here TMyFruits includes the values frApple and frPear. For... To loop with enumerated variableDelphi allows us to work with the elements of an enumerated type using an index that comes from the order that they were listed in. In the following example, dirNorth in the dirNorth type declaration has the index 0, dirEast has the index 1, and so on. Now you can write something like: procedure TForm1.Button1Click(Sender: TObject); type dirNorth = (dirNorth, dirEast, dirSouth, dirWest); var Dir: TDirection; begin for Dir := dirNorth to dirWest do ShowMessage('Ordinality: ' + IntToStr(Ord(Dir))); end; Pitfalls1. It's best to choose constant names that are not likely to conflict with other identifiers. Examples: type TSound = (tsClick, tsClack, tsClock); TMyColor = (mcRed, mcBlue, mcGreen, mcYellow, mcOrange); TAnswer = (ansYes, ansNo); 2. You can use the (Val1, ..., Valn) construction directly in variable declarations, as if it were a type name: var Dir1: (dirNorth, dirEast, dirSouth, dirWest); But if you declare Dir1 this way, you can not use the keyword var again to declare another enumerated variable with the same constant identifiers. Thus:
var Dir1: (dirNorth, dirEast, dirSouth, dirWest); var Dir2: (dirNorth, dirEast, dirSouth, dirWest); generates a compilation error. But:var Dir1, Dir2: (dirNorth, dirEast, dirSouth, dirWest); compiles fine. Also the following causes no error: type TDirection = (dirNorth, dirEast, dirSouth, dirWest); var Dir1: TDirection; Dir2: TDirection; 3. The use of constant expressions in subrange definitions introduces a syntactic difficulty. In a type declaration, when the first meaningful character after = is a left parenthesis, the Delphi compiler assumes that you are defining an enumerated type. Hence the code:
const X = 50; Y = 10; type TScale = (X - Y) * 2..(X + Y) * 2; produces an error. Work around this problem by rewriting the code, move the first parenthesis more to the right:
type Scale = 2 * (X - Y)..(X + Y) * 2;
|
|
Database Tutorials FAQ Crash Course Delphi Tips Source Code Downloads Links
© Copyright 1999-2019
DelphiLand