Difference between revisions of "Basic Pascal Tutorial/Chapter 5/Enumerated types"

From Lazarus wiki
Jump to navigationJump to search
m (bypass language bar/categorization template redirect [cf. discussion])
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Enumerated types}}
+
{{Basic Pascal Tutorial/Chapter 5/Enumerated types}}
{{TYNavigator|Solution_4|Subranges}}
+
{{TYNavigator|Chapter 4/Solution|Chapter 5/Subranges}}
  
 
5A - Enumerated Types (author: Tao Yue, state: unchanged)
 
5A - Enumerated Types (author: Tao Yue, state: unchanged)
  
 
You can declare your own ordinal data types. You do this in the type section of your program:
 
You can declare your own ordinal data types. You do this in the type section of your program:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
  datatypeidentifier = typespecification;
 
  datatypeidentifier = typespecification;
Line 14: Line 14:
  
 
For example, if you wanted to declare the months of the year, you would do a type:
 
For example, if you wanted to declare the months of the year, you would do a type:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   TMonthType = (January, February, March, April,May, June, July, August, September, October, November, December);
 
   TMonthType = (January, February, March, April,May, June, July, August, September, October, November, December);
Line 20: Line 20:
  
 
To make months starting from 1:
 
To make months starting from 1:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   TMonthType = (January=1, February, March, April,May, June, July, August, September, October, November, December);
 
   TMonthType = (January=1, February, March, April,May, June, July, August, September, October, November, December);
Line 26: Line 26:
  
 
You can assign more than one enumeration per value:
 
You can assign more than one enumeration per value:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
type
 
type
 
   TMonthType = (January=1, FirstMonth=1,February=2, SecondMonth=2...);
 
   TMonthType = (January=1, FirstMonth=1,February=2, SecondMonth=2...);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Or the example below will result in January=1, February=2, May=5,June=6, Jule=7:
+
Or the example below will result in January=1, February=2, May=5,June=6, July=7:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
type
 
type
 
   TMonthType = (January=1, February, May=5,June, July);
 
   TMonthType = (January=1, February, May=5,June, July);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
  
 
You can then declare a variable:
 
You can then declare a variable:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
var
 
var
 
   Month : TMonthType;
 
   Month : TMonthType;
Line 46: Line 44:
  
 
You can assign any enumerated value to the variable:
 
You can assign any enumerated value to the variable:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
Month := January; or Month := TMonthType(0);
 
Month := January; or Month := TMonthType(0);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 59: Line 57:
  
 
To walk over the enumerated definition:
 
To walk over the enumerated definition:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
for i := Ord(Low(TMonthType)) to Ord(High(TMonthType)) do
 
for i := Ord(Low(TMonthType)) to Ord(High(TMonthType)) do
 
   begin
 
   begin
Line 66: Line 65:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
{{TYNavigator|Solution_4|Subranges}}
+
{{TYNavigator|Chapter 4/Solution|Chapter 5/Subranges}}

Latest revision as of 15:20, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

5A - Enumerated Types (author: Tao Yue, state: unchanged)

You can declare your own ordinal data types. You do this in the type section of your program:

type
 datatypeidentifier = typespecification;

One way to do it is by creating an enumerated type. An enumerated type specification has the syntax:

(identifier1, identifier2, ... identifiern)

For example, if you wanted to declare the months of the year, you would do a type:

type
  TMonthType = (January, February, March, April,May, June, July, August, September, October, November, December);

To make months starting from 1:

type
  TMonthType = (January=1, February, March, April,May, June, July, August, September, October, November, December);

You can assign more than one enumeration per value:

type
  TMonthType = (January=1, FirstMonth=1,February=2, SecondMonth=2...);

Or the example below will result in January=1, February=2, May=5,June=6, July=7:

type
  TMonthType = (January=1, February, May=5,June, July);

You can then declare a variable:

var
  Month : TMonthType;

You can assign any enumerated value to the variable:

Month := January; or Month := TMonthType(0);

All the ordinal functions are valid on the enumerated type. ord(January) = 0, and ord(December) = 11. Or if the Month variable should go to next calendaristic month a call to Succ(Month) can be made. Consider verifying with Month < High(TMonthType) that there is a next month. Get number of elements in enumeration with ord(High(TMonthType)) + 1.

A few restrictions apply, though: enumerated types are internal to a program -- they can neither be read from nor written to a text file. You must read data in and convert it to an enumerated type. Also, the identifier used in the type (such as January) cannot be used in another type.

One purpose of an enumerated type is to allow you, the programmer, to refer to meaningful names for data. In addition, enumerated types allow functions and procedures to be assured of a valid parameter, since only variables of the enumerated type can be passed in and the variable can only have one of the several enumerated values.

Write and Writeln can be used to print a string representing the current value in an enumerated type variable. WriteStr(string-var,enum-var) can be used to place a string representation of the enum value into a string, or in one shot GetEnumName(TypeInfo(TMonthType), Ord(Month)) if typinfo unit is added in uses clause.

To walk over the enumerated definition:

for i := Ord(Low(TMonthType)) to Ord(High(TMonthType)) do
  begin
     x:= GetEnumName(TypeInfo(TMonthType), Ord(i));
  end;
 ◄   ▲   ►