Subrange types)

Source: Internet
Author: User

Subrange types)Defines a subset of an ordered type. The ordered value of the subfield must have been defined in another ordered type. The index of the ordered value in the subfield is determined by the base class.

It is in the formLow... high. HereLowAndHighIs a constant expression of the same order type, andLowRatioHighSmall.

When defining a sub-sector type variable, the value assigned to the variable must be a value within the defined range of the sub-sector.

Let's give a detailed example.

1,CreateConsole Application;

2,Enter the following code. Pay attention to the notes.

Program project1; {$ apptype console} uses sysutils; const x = 50; y = 10; Type Scale = 2 * (x-y ).. (x + y) * 2; {constant expression} // scale = (x-y) * 2 .. (x + y) * 2; {this statement and the above low .. the high range is the same, but the comments will cause compilation errors. In any type declaration, if the equal sign (=) is followed by a left parentheses (), the compiler considers it to be an enumeration type. If the above declaration is not an enumeration, an error will occur, avoid declaring that it is not an enumeration type. The left parentheses appear after the equal sign} tcolors = (red, blue, green, yellow, orange, purple, white, black ); {enumeration} tmycolors = green .. white; {defines a subfield type, ranging from green to white in enumeration} {tmycolors includes green, yellow, orange, purple, white} tsomenumbers =-128 .. 127; {subfield type of number range} TCAPs = 'A '.. 'Z'; {subfield type of the character range} var somenum: 1 .. 500; {You can also directly define the subinterface type variable} mycolors: tmycolors; somenumbers: tsomenumbers; caps: TCAPs; letter: Char; begin // caps: = 'E '; {this sentence will cause the compilation error "constant expression violates subrange bounds" out of the range} letter: = 'E'; caps: = letter; {the above two sentences can be compiled, however, an error occurs when you run the following comments. The code is out of the subfield} // writeln (the index of 'caps in TCAPs is: '+ inttostr (ord (CAPS); mycolors: = green; writeln ('green's index in tmycolor is:' + inttostr (ord (mycolors); readln; end.

3. F9After running, verify the result as follows:

The above code passes the test in Delphi7. Download the sample code:20151124subinterface certificate (subrange_types#.rar

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.