delphi7 Learning: Data Types

Object Pascal language provides a wealth of data types, i.e. simple types (the Simple), string type (String), junction
structure type (StructEntryTable), pointer type (the Pointer), the function and the type of process (Procedural) type and variants ( Variant) and so on.

(1) simple type (the Simple)
Simple types include real types (Real) and ordered type (Ordinal). Ordered type also includes integer type, character type, cloth
Seoul types, enumerated types and subrange types. Their relationship is shown in Figure 4-1.
Figure 4-1 simple type diagram
① integer type
integer type include a type, as shown in Table 4-5.
Table 4-5 Type Integer
Integer range Remarks Type
-2 1,474,836,482,147,483,647 32-bit signed ~ Integer
Cardinal 994 967 295 0 ~ 42 is the number of 32-bit unsigned
Shortint -128 ~ 127 8-bit signed number
Smallint - 32768 ~ 3276716-bit signed number
Longint -2 147 443 648 ~ 2 147 483 647 32 -bit signed number
Int64 -2 ~ 2-1 64-bit signed number
Byte 0 ~ 255 8-bit unsigned

Word 0 ~ 65 535 16 bit unsigned

Longword 0 ~ 4 294 967 295 32-bit unsigned

② character types
of characters There are three types of character variables, as shown in Table 4-6.
Table 4-6 Character Types

Character type
Value
Remarks

AnsiChar
. 8 bit
according to an ANSI character table arrangement

WideChar
16 bit
according to Unicode code arrangement, identical with the first 256 characters in ANSI character Unicode

Char
Base on Delphi in common character type, the equivalent of WideChar

Note: Since a character does not indicate a byte length, the program must be used if the number of bytes of characters, preferably
with the SizeOf () function to get.
③ Boolean
Boolean type includes 4, respectively, Boolean, ByteBool, WordBool and LongBool. Boolean ByteBool and each
one byte, WordBool occupies two bytes, LongBool 4 bytes.
Boolean value can only be 0 (False) or 1 (True), ByteBool, WordBool LongBool and there may be an integer symbol
number, 0 represents False, non-zero values represent True.
The most common type is Boolean, and ByteBool, WordBool and LongBool type for Windows with other languages and
is compatible with the environment, because at the time of the Windows API returns a Boolean value, its value may be a two-byte signed integer. If the DUT
FIG assign the return value Boolean data type, the compiler does not match that type, if the type-casting, may make return
valid data value is truncated.
④ enum type
enumerated type is an ordered set of identifiers, which lists the values of this type may comprise of all. For example, one week
, 7 days program respectively 0, 1, 2, 3, 4, 5, 6 represent each day for a week, but the program is very intuitive. Here
is a statement of an enumerated type code:
of the type
DayOfWeek = (Sunday, Monday, Tuesday , Wednesday, Thursday, Friday, Saturday); // variable name = (enumerated list)
wherein the list of identifiers each other are separated by commas, can be enumerated type that lists range of values. Object Pascal rules
set, a first identifier is 0, the second identifier value of 1, and so on.
To refer to this enum type, you must declare a variable of type DayOfWeek, for example:
var
Days: DayOfWeek;
This declares an enumeration type DayOfWeek Days, Days of the value of the identifier is always a list.
To simplify the process, it may also be combined type declarations and variable declarations, such as:
var
Days: (the Sunday, catalog on Monday, on Tuesday, catalog on Wednesday, catalog on Thursday, catalog on Friday, Saturday);
Note: only enumerated element identifier, naming identifiers must comply with the rules on Object Pascal identifier
given. For example, can not begin with a number, the identifier can not be a reserved word, with an enumeration element can not occur simultaneously in more than
one enumeration.
Delphi 7 programming and development technology Daquan
· 58 ·
⑤ subrange
subrange type is a value within a certain range of the following types, namely, integer, Boolean, or enumeration type character. If you need a variable restriction
when the amount of range, the subrange is a good choice. For example:
type
Month = 1..12;
Letters=’A’.. ’H’;

DayOfWeek=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
//枚举型

Monday..Friday = Days;
// a subrange-type DayOfWeek

Like enumerated types, sub-sector is not directly involved in the type of operation, you must declare a variable of type sub-sector.
Note: When you declare and use of sub-sector type, type constant upper bound and lower bounds constants must be consistent, and are orderly type. Sub-
sector variables all participating host operations may reference its type. The value of the sub-sector is variable range, if
the process of operation outside this range will cause an error.
Delphi documents mentioned in the sub-sector type of upper and lower bounds can be an expression.
⑥ Real
Real type is not orderly, because its range is infinite, real data is also called real numbers, real used to represent commonly referred to as floating
points. Real number data (Real) Object Pascal language type as shown in Table 4-7.
Table 4-7 Type real
real types range of significant digits bytes
Real48 2.9 * 10 * 1011 ~ 12 is ~ 1.7. 6
Single for 1.5 * 3.4 * 107 ~ 10 ~. 4. 8
Double 5.0 * 1.7 * 1015 ~ 10 ~ 16. 8
Extended 3.6 * ~ 1.1 * 1019 ~ 10 20 is 10
Comp. 1 -2 +. 8 ~ 2-1. 19 ~ 20 is
the Currency -922337203685477.5808 ~. 19 ~ 20 is 922,337,203,685,477.5807. 8
real 5.0 * 1.7 * 1015 ~ 10 ~ 16. 8
wherein the common real number type real, Single , Double.
(2) Type String (String)
Object Pascal string type and the long string is divided into two kinds of short strings.
Short strings, refers to the maximum string length does not exceed 255 characters in the character sequence. When the state of the switch when compiling {$ H-}, with the
reserved word String is a short string declaration, compiler switch $ H regardless of what is in a state with a declared variable ShortString
always shorter string type. Code is as follows:
var
MyStr1: a ShortString;
MyStr2: String [200];
enclosed in the square brackets reserved words String unsigned integer indicating the maximum length of the string 200. When a character string
when the character type is assigned a short variable exceeds its maximum length will be truncated.
Each of the short string of characters can be accessed by name plus character string index. Index is zero, but the index of 0
is stored at the actual length of the string, the function of the two lines is the same, the real beginning of the character at index 1.
Ord (Str [0]); // Str stands for short string
Length (Str);
shorter string is dynamically changed, can be made using the Low and High Function minimum number and maximum number string,
the return value is Low is 0, High return value is the maximum length of the string declared.
Length of long strings is almost unlimited, it can theoretically reach 2GB, actually only limited by your computer's memory. To sound
Chapter 4 Object Pascal language
· 59 ·
out a long string, can be changed by a predefined identifier AnsiString or compile switch $ H {$ H +} after the reserved word String Acoustic
described.
After a long string statement is automatically initialized to an empty string. Each character in the string length, is 1-based index of the long
string of no index 0, to get the actual length of the string, it can only be called Length () function.
In its long string after the last automatic character string representing the end of a character NULL (NULL characters long character is not
part of the string), the character pointer that type (PChar) Similarly, the thus converted to a long string PChar very convenient type,
syntax is: PChar (Str), wherein the length of the string Str representatives. Long string is converted to a pointer to a character string of each,
even if the length of the string is empty, after the cast is always possible to obtain a pointer to NULL character in the character end pointer.
Object Pascal string and allows also appear in a long string expression, the compiler will automatically converted as needed.
Note: a pair of string variables must be enclosed in single quotes, single quotes if the string itself, the single quotation marks to use
two consecutive single quotes expressed as: 'His name is' 'Jim ' ''.
(3) Structure Type (StructEntryTable)
structure data type comprising: a collection type, an array type, record type, file type, class type, type and class reference interface class
type.
① collection type
set type is a group with a combination of elements of the same type, the type must be limited types, such as integer, boolean, character
type, enumerated types and subrange types.
Collection type of method statements, see the following code:
of the type

= SET of setl 'A' .. 'the Z';
Set2 of Char = SET;
// group type is a subrange type
// character group type

Set3 = set (Jan, Feb, Mar, Apr); // group type is an enumeration type
Object Pascal language is provided for a set of several operators, these operators can run Analyzing the relationship between the set of the set,
e.g. the collection element deletions, intersection of operation and the like.
Assignment operator simple types, the declaration of a set of variable type, set to a specific value for this variable, for example,
such as:
var
TINT1, TINT2: SET of 1..10;
code above, although the two sets of statements variable, but does not mean TINT2 variables TINT1 and collection of 1 to 10, must
assign them:
TINT1: = [1,3,5,7,9];
TINT2: = [2..5,8..10 ];
this is a collection of TInt1 1, 3, 5, 7, 9 composition, is a collection of TINT2 2, 3, 4, 5, 8, 9, 10 thereof.
Relational operation is a collection of types of operations, follow mathematical rules on set operations, the result of the operation is a Boolean value. Set
bonding operation is to determine the relationship between the two set relationship to each other, comprising relational operators: = (equal to), <> (not equal),> = (large
than or equal to), <= (less than or equal).
A collection set equal to B, means that two elements of a set exactly the same (irrespective of the order of elements), otherwise it's not
equal to, the following:
[1,2,3,4] = [2,4,1,3 ]
[1,2,3,4] <> [5,8,9]
If the collection element B can be found in the set a, the set a is greater than said set equal to B, and vice versa, said large collection B
Set equal to A, as follows:
[1,2,3,4]> = [l, 2,3]
Delphi program design and development techniques. 7 Encyclopedia
· 60 ·

Note: Object Pascal can not use> or <relational operation of the set type. Because the type is not an ordered collection type,
you can not function with the Ord, Pred, Succ like.

② array type
array type is an ordered combination of a data type, wherein a value of each element is specified by its relative position in the array can be
placed in a location on the data, and use the data when needed. The following code declares a variable array of type Integer:
var
TInt: Array [1..15] of Integer;
represents TInt containing data pointing to a 15 element tandem type Integer, representative of each element is between 1 and 15 number,
called an index. Each array "[]" in the data set name with the index represented. TInt contains 15 variables, TInt [. 1] representative of a first
variable, TInt [2] represents the two variables, and so on.
Array type definitions can also be used, as follows:
type
Int = Array [1..15] of Integer;
var
TInt: Int;
may assign an array using the following procedure:
for the I = 0 to 15 do
TInt [the I]: = 0; // array each element is given the initial value 0
array can be multi-dimensional, and the assignment is defined as follows:
type
Tarr = array [1..15,1..15] of Integer;
var
Arr: Tarr;
C , R & lt: Integer;
......
for C: = 15 to do. 1
for R & lt: =. 1 to 15 do
Arr [C, R]: = 0; // multidimensional array each element is given the initial value 0
......
③ record type
record the program can be set into a set of data access group. Example, the following code illustrates a record type definition Method:
type
TStudent = Record
ID: Integer;
the Name: String [10];
Age: Integer;
the Memo: String [100];
End;
record contains the domain data can be stored, each a field has a data type. TStuednt type declared in the code above 4
domain. Carried out with the following types of recording methods using variable declarations:
var
Stu: TStudent;
......
with Stu do // The following code record assignment may be recorded only to a single domain assignment
Chapter 4 Object Pascal language
· 61 ·
the begin
ID: =. 1;
the Name: = 'Jim';
Age: = 18 is;
the Memo: = 'a Good Student iS of He.';
End;
......
(. 4) pointer type (the pointer)
pointer is a variable memory address represents . Pointer types are more difficult to understand in any language, but also a number of more flexible
data type. Pointer variable is defined in the following form:
type
pointer type identifier Type = ^ yl;
The following example include a pointer operation code is as follows:
Program Point; // pointer exemplary project
{} $ the Apptype Console
type
PINT = ^ Integer; // define a pointer type
var
the I, K : Integer;
the PI: Pint; // int pointers
P: pointer; // pointer to void
the begin
the I: =. 3;
K: =. 5;
the PI: @ = K; // pointer to plastic shaping data
writeln ( 'K =' , the PI ^);
P: the I = @;
K: = Integer (P ^); // pointer to non-integer data type
writeln ( '= K', K);
the PI: = P; // pointer assignment between
writeln ( 'the PI ^:', the PI ^);
Readln;
end.
end of the run, shown in Figure 4-2.
Figure 4-2 shows the result pointer example project
Hint: See the examples in this book supporting the CD "Section4 \ Pointer \ Point.dpr" file.
Delphi 7 program design and development techniques Encyclopedia
· 62 ·
"@" operator in front of the variable, the acquired address is variable and address can be assigned to the same type of data pointer.
The "^" operator placed in front of the data types can be defined the type of a pointer type; a pointer on the back, can be obtained
to obtain the contents of the pointer address space.
For example, in "Point", the definition of a data type may point to any type of non-pointer to P, but use of the process is to be noted class
type conversion, the contents can not be referred to directly address directly assigned to other types of variables.
Object Pascal language is defined in some standard pointer can be called directly, as shown in Table 4-8.
Standard Pointer Table 4-8 Object Pascal Language

Pointer type
type variable points

PString, PAnsiString
AnsiString

PByteArray
ByteArray

PCurrency
Currency

PExtended
Extended

POleVariant
OleVariant

PShortString
ShortString

PVariant
Variant

PVarRec
TVarRec (System unit to be cited when calling)

RTextBuf
TextBuf (SysUtils unit to be cited when calling)

PWideString
WideString

PWordArray
TWordArray (SysUtils unit to be cited when calling)

(5) Function and process type (Procedural)
Delphi is object-oriented and event-driven, so that before a top-down structured design method is not suitable in Delphi
are programmed. Functions and procedures are two subroutines Pascal language, they can always be called. When the function returns a value executed, and
the procedure is executed, there is no return value. Calling function is to obtain a return value, the calling process to perform an operation only.
Functions can only appear in the expression can not be used alone; the process can not occur in an expression, only in the form of a statement.
① function using
the function must be declared before calling. Function declaration syntax is as follows:
function function name (<formal parameter list>): <type>;
the begin
<statement>;
<statement>;
...
<statement>;
End;
syntax as a function call:
<function name > (<argument list>);
NOTE: the actual argument list of the parameters must be separated by commas, and the order must function corresponding to the order described, and a
same number, match type.
Below cite a simple example of a function call, to write a custom function named Total in the program, the main function is to pass
two parameters were calculated by adding up, returns the calculated results is completed. The main code is as follows:
Program FunctionDemo;
{} $ APPTYPE the CONSOLE
uses
the SysUtils;
Chapter 4 Object Pascal Language
· 63 ·
// defined functions for calculating a, b and the parameter
function the Total (a, b: Integer): Integer;
the begin
the Total: = A + B;
End;
var
a, b, C: Integer;
the begin
the try
writeln ( 'enter a value');
Readln (a);
writeln ( 'enter value b');
Readln (b);
C: = the Total (a, b); // call the function
Write ( 'a to b and it is: ');
Writeln (c);
Readln;
the except
Writeln (' content is not digital input ');
End;
End.
Note: see examples in this book supporting the CD "Section4 \ Function \ FunctionDemo.dpr" file.
② process of using
procedures and functions similar, must be declared before calling. Procedure declaration syntax is as follows:
proceduce <procedure name> (<formal parameter list>)
the begin
<statement>
<statement>
...
<statement>
End;
syntax procedure calls for:
<Procedure name> (<argument list>)
(6) types of variants (the Variant)
type of variant type capable of dynamically changing during operation. Variants can support all types of simple data types, such as integers, floating point
, string, Boolean, date, time, currency, and OLE Automation objects.
Note: do not express a variant Type Object Pascal object.

Guess you like

Origin www.cnblogs.com/huiy/p/12334285.html