The enumeration type of Delphi

Source: Internet
Author: User
Tags ord

Reference: http://blog.csdn.net/kissdeath/article/details/2060573

Delphi programs can be used not only for numerical processing, but also for processing non-numeric data more broadly. For example: Gender, month, day of the week, color, unit name, education, occupation and so on.

1. Definition of enum type

Format: type enum Types identifier = (identifier 1, identifier 2, ..., identifier N)

2. Enumeration type Data features

1) enumeration elements can only be identifiers

For example, the following definition is legal

Type days     = (Sun, Mon, Tue, Wed, Thu, Fri, Sat);    colors= (red, yellow, blue, white, black, green);

And the following type definition is wrong

Type    colortype= (' Red ', ' yellow ', ' blue ', ' white ');    Numbers= (1,3,4,5,6);

When defining an enumeration type, all enumeration elements are listed to form the value range of this enumeration type.

2) enum type is in order type

Their ordinal number is determined by the sort order of each enumerated type when the type is defined, and the sequence number starts at 0

For example, define

Type     days= (SUN,MON,TUE,WED,THU,FRI,SAT);

Then, Ord (Sun) =0, Ord (Mon) =1,..., etc.

The first element in an enumeration type has no predecessor, and the most one element has no successor

3) The same enumeration element cannot appear in two or more than two enumeration type definitions. The definition below is incorrect.

Type    Color1 = (red, yellow, white);    Color2 = (blue, red, black);

Because red belongs to enum type Color1 and enum type Color2

4) enumeration Type variables can only be assigned and relational operations, not arithmetic and logical operations

When the enumeration element is compared, it is actually a comparison of its ordinal number

For example, define the following

Type days    = (Sun, Mon, Tue, Wed, Thu, Fri, Sat);    Colors = (red, yellow, blue, white, black, green); var    color:colors;    Weekday:days;

Then the following statement is legal

Weekday: = Mon;if Weekday=sun then    write (' Reset ');

And the following statement is not legal

Mon: = 1;    Mistake the enumeration value as the variable name weekday: = blue;     Enumeration value Blue does not belong to the domain of the enumeration variable weekday read (color);    Enumeration type variables cannot be assigned with a read statement (weekday); Writeln (blue);        Variable values and enumeration values for enum types cannot be output through write statements

5) You can combine the description of a variable with the definition of a type, such as

var    hoilday, Workday: (Sun, Mon, Tue, Wed, Thu, Fri, Sat);    Color: (red, yellow, blue, white, black, green);

The input and output of the enumeration data can be done by indirect means. Input, you can generally enter a code, through the program to convert, output, but also print out the enumeration element corresponding to the string. This will be shown in the following example

3. Enumeration types of applications

Example, enter today is the day of the week ordinal, the output tomorrow is the day of the English word (Sunday serial number is 0)

Type    weekday = (Sun, Mon, Tue, Wed, Thu, Fri, Sat); var    i:integer;    Today, Tomorrow:weekday;begin    writeln (' What's date is it ');    READLN (i);    Case I of        {converted from input to enum type}        0:today:=sun;        1:today:=mon;        2:today:=tue;        3:today:=wed;        4:today:=thu;        5:today:=fri;        6:today:=sat;    Endl        If Today=sat then        tomorrow: = Sun    else        Tomorrow: = SUCC (today);    SUCC () is a subsequent    write (' The Tomorrow is ') that asks for this enumeration element;    Case Tomorrow of        Sun:writeln (' Sunday ');        Mon:writeln (' Monday ');        Tue:writeln (' Tuesday ');        Wed:writeln (' Wednesday ');        Thu:writeln (' Thursday ');        Fri:writeln (' Friday ');        Sat:writeln (' Saturday ');    End;end.

An enumeration type is an ordered type, so a variable of the enumeration type can be used as a loop variable.

When learning enum types, note the differences between enumeration elements and variables, and the handling of input and output methods for enumerations and variables

The enumeration type of Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.