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

From Lazarus wiki
Jump to navigationJump to search
Line 2: Line 2:
  
 
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:
 +
<delphi>
 
type
 
type
 
  datatypeidentifier = typespecification;
 
  datatypeidentifier = typespecification;
 +
</delphi>
  
 
One way to do it is by creating an enumerated type. An enumerated type specification has the syntax:
 
One way to do it is by creating an enumerated type. An enumerated type specification has the syntax:
Line 9: Line 11:
  
 
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:
<font color="#006699"><strong>type</strong></font>
+
<delphi>
  MonthType = (January, February, March, April,
+
type
                May, June, July, August, September,
+
  MonthType = (January, February, March, April,
                October, November, December);
+
              May, June, July, August, September,
 +
              October, November, December);
 +
</delphi>
  
 
You can then declare a variable:
 
You can then declare a variable:
<font color="#006699"><strong>var</strong></font>
+
<delphi>
  Month : MonthType;
+
var
 +
  Month : MonthType;
 +
</delphi>
  
 
You can assign any enumerated value to the variable:
 
You can assign any enumerated value to the variable:
Month := January;
+
<delphi>
 +
Month := January;
 +
</delphi>
  
 
All the ordinal functions are valid on the enumerated type. <tt>ord(January) = 0</tt>, and <tt>ord(December) = 11</tt>.
 
All the ordinal functions are valid on the enumerated type. <tt>ord(January) = 0</tt>, and <tt>ord(December) = 11</tt>.

Revision as of 17:54, 5 January 2010

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: <delphi> type

datatypeidentifier = typespecification;

</delphi>

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: <delphi> type

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

</delphi>

You can then declare a variable: <delphi> var

 Month : MonthType;

</delphi>

You can assign any enumerated value to the variable: <delphi> Month := January; </delphi>

All the ordinal functions are valid on the enumerated type. ord(January) = 0, and ord(December) = 11.

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.

previous contents next