Basic Delphi type

Source: Internet
Author: User

Delphi7 uses the Windows development language, so it uses the working mechanism of windows.

Its core is three:

1) window: the operating system continuously monitors the activity or event signals of each window.

2) Event: it can be a user event or a system event.

3) message: each time an event occurs, a message is sent to the operating system.

An object is an object with certain attributes, methods, and events. We can regard attributes as the properties of objects, methods as the actions of objects, and events as the responses of objects.

 

The Delphi project consists of two parts:

The first part is pascal's Code. Its save is. pas.

The second part is the project part. Its storage method is. dpr.

 

Structure of a code unit:

Unit FirstUnit; {1 unit header, indicating that the unit file name is FirstUnit. pas}

Interface {2 interface definition}

Uses {unit used}

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

Type {type declaration part}

TForm1 = class (TForm)

Button1: TButton;

Label1: TLabel;

Edit1: TEdit;

Procedure Button1Click (Sender: TObject );

Private

{Private declarations}

Public

{Public declarations}

End;

Var {Variable declaration part}

Form1: TForm1;

Implementation {3 implementation part}

{$ R *. dfm} {compiler instruction for searching resources}

Initialization {4 initialization part}

Finalization {5 end part}

End.

Generally, the unit file consists of the unit header, interface, implementation, initialization, and finalization) five components.

1. unit: indicates the file name of the unit. The unit name must have the same name as the file name.

2. interface: Declares the type, constant, variable, function, and process. by uses, the type declaration (type), the variable Declaration (var) Interface part can only declare not to put the implementation. the description of visible domains will be described later!

3. implementation part (implementation): contains the function and Process Code declared in the interface part.

4. initialization: runs only once at the beginning of the program, which is equivalent to the constructor of C ++.

5. finalization: This is run when the object ends. It is equivalent to a C ++ destructor and is a constructor.

Reverse structure process.

 

Program debugging:

Delphi7 provides a powerful Integrated Debugger)

 

Exception Handling:

"Generate exception" indicates that an error occurs during the program running and an exception object (including the error information and time and location) is created to respond to the error. the base class of the Exception object is Exception, which is defined in the SysUtils unit.

1. structured exception handling

The basic structure is try... begin T. end;

Try

Test code statement;

Except

On exception 1 do exception 1 response statement

On exception 2 do exception 2 response statement

.......

End;

The code that may contain errors may be placed behind the keyword try, and the exception handling will be placed behind the limit T.

2.

Try... Finally

Finally runs regardless of whether the try is abnormal.

 

 

Delphi_day02

Constants, variables, and Data Types

I. Data Type:

1. Value Type: used to store data of various numerical forms. There are two types: integer and real

Integer byte count real Byte Count

Integer 4 Single 4

Optional int 1 Real 8

Smallint 2 Double 8

Longint 4 Currency 8

Int64 8 Extended 10

Byte 1 Read48 6

Word 2 Comp 8

Longword 4

Cardinal 4

 

2. Character and string types

Character types are used to store data of various character forms. They are classified into two categories: character type and string type.

Bytes maximum length of string type

Char 1 String 4 -2G

AnsiChar 1 serial string 2-256

WideChar 2 AnsiString 4 -2G

WideString 4 -2G

3. Logical type

Logical type, also known as Boolean, is a data type used to store logical values.

Number of logical bytes

Boolean 1 0 (false) 1 (true)

ByteBool 1 0 (false )! 0 (true)

WideString 2 0 (false )! 0 (true)

LongBool 4 0 (false )! 0 (true)

 

4. Forced type conversion:

ObjectPascal is a strongly typed language. It does not directly assign values to data of different data types, and one data type cannot be directly involved in other types of operations.

Function of type conversion

IntToStr (Value: Integer) converts Integer and long Integer data into strings respectively.

IntToStr (Value: Int64) string

FloatToStr (Value: Extended) converts Real data into string data

StrToFloat (const S: string) converts string-type data to Real-type Real

StrToInt (const S: string) converts string data into integer and long integer respectively.

StrToInt64 (const S: string)

The above conversions are performed from a byte or a type with a relatively large length as a parameter. Here we want to force the type conversion service.

Note: When a long type is converted to a short type, the system automatically intercepts the data, which may affect the data precision. for different types of data, if you want to convert a strong data type to a weak data type, you need to use the Trunc (a parameter) or Round (a parameter) truncation function)

2. constants and variables:

1. There are two types of data in a program: one is the constant value during the program running, and the other is the variable.

Identifier:

Other characters starting with letters or underscores (_) can be numbers and letters.

Case Insensitive.

Constants include: constant constants and symbolic constants.

Symbol constant:

Const

<Constant name 1>: [<type Name>] = <constant value 1>

.........

You do not need to write the type name. Note that the constant value assignment is not the same as the normal value assignment, and the constant value assignment uses the "=" rather than ": ="

2. variables:

The type of the variable determines the size and format of the corresponding memory space. The value of the variable is the data value stored in the corresponding memory space. The variable must be declared before it can be used.

Var

<Variable name 1>: <type name 1>

............

Note: The definition of variables must be separated by commas (,) in the special constant and variable definition area of the event process, that is, before the first in, when the same row declares the same type, A variable has a fixed value before it can be involved in various operations. Pay attention to the effective wildcard.

 

Iii. Scope:

The scope of an identifier ranges from small to large, including three levels: Process Level, unit level, and program level.

The internal variable of the process, that is, the variable before begin, only acts on the process.

The variable written in implementation only acts on this unit

The interface variable can act on the units under all work items, so that the other variables to be referenced are added to the uses clause, and then referenced by <unit name>. <identifier>.

Redefines the identifier, that is, the identifier used in a larger scope has a new identifier with the same name within it, such as the process level. This identifier will temporarily hide the identifier in a larger scope.

 

Iv. Advanced data types:

1. Definitions and declarations of enumeration types and application functions:

First, define the enumeration type:

Type

<Enumeration type name >=( <identifier 1>, <identifier 2>, <identifier 3>, <identifier 4> ,......);

Note: type is a reserved word of the system, indicating the beginning of the definition of advanced data types. <Enumeration type Name> is the name of your own Enumeration type, <identifier> lists all data elements of this type, which are called enumeration constants. Enumeration constants are separated by commas. Repeated definitions are not allowed.

Statement:

Var

<Identifier Name>: <Enumeration type Name>;

Function Application:

The identifiers in the enumeration are sequential from 0 to the beginning, so there is a set of functions in the enumerated values:

As follows:

Enumeration function call format

Ord calculates the enumeration ordinal number ord (enumeration constant or enumeration variable)

Pred evaluate the precursor value...

Succ calculates the successor value...

Low calculates the first enumerated constant Low (Enumeration type name)

Hight does not obtain an enumerated constant ....

 

2. Definition and declaration of subinterface types and note

A subfield type is a set of data types with fixed values. The subfield type must be a subset of a certain sequence type, so its corresponding sequence type is of the base type.

Definition:

Type

<Subinterface type name >=< constant 1>... <Constant 2>;

Note: <subinterface type Name> is the name of a user-defined subinterface type. <constant 1> indicates the lower bound of the subinterface type, that is, the minimum value, <constant 2> indicates the upper bound of the subinterface type, that is, the maximum value. The subinterface must be smaller than the lower bound and must be of the same type.

Example:

Lowercase = 'A'... 'Z ';

 

Month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec );

Sping = Feb... May;

Statement:

Var s1, s2: spring;

Note:

The values assigned by the subinterface In the debugging phase are checked. The values in the subinterface in the running phase may not be checked.

3. Set Type:

The set here is a mathematical set definition. It has the following features:

1) the opposite sex. Elements in the set can be differentiated from each other. Identical elements are considered as the same element.

2) the elements in the set are not ordered and can be arranged randomly.

3) finite. In general, the number of set elements can be limited, but the number of set elements cannot exceed 256.

Set definition:

Type

<Set type Name> = set of <base type>;

<Set type Name> indicates the name of the user set type, and <base type> indicates the Data Type of each element in the set. Here: The <base type> of the set type must be the sequence type, logical type, enumeration type, and subfield type, but not the integer type and other data types. At the same time, the number of elements cannot exceed 256.

For example:

Type

Uppercase = 'A'... 'Z ';

Uchars = set of uppercase;

Month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec );

Vacation = set of Month;

Declaration of set variables:

Var

SumVac, WinVac: vacation

You can also use the subinterface without definition.

Var

N1, n2: set of 1 .. 100;

Value of the Set variable:

Unlike other variables, a set variable is not a separate element, but a set of elements. The value of a set variable is called a set value. The basic expression is as follows:

[<Element 1>, <element 2>, <element 3>, <element 4>...];

Calculation of the set type:

It is actually a mathematical intersection, merger, and difference operation.

 

The operation name indicates whether the calculation result meets the exchange law.

Calculate all non-repeating element sets in the S1 + S2 collection

The element set in S1 * S2 is

Differential computation S1-S2 all sets that belong to S1 not S2 No

Equality operation S1 = S2 element all equal true Yes

Unequal operation S1 <> S2 elements are not equal. true is

All elements that contain the operation S1> = S2 are in S1. true no

Included operation S1 <= S2 No

X in S1 element X true in S1 set no

 

4. array type:

An Array is an ordered set of several elements with fixed numbers and the same type. The provided data is divided into static arrays and dynamic arrays.

A) Static Array

Static arrays must be allocated memory units during program initialization to specify their fixed size and element data types.

Definition:

Type

<Array type Name> = array [<subscript set 1>, <subscript set 2>] of <base type>

<Subscript set 1> indicates the size of the first dimension of the array, and <subscript set 2> indicates the size of the second dimension of the array ......

For example:

Type

MyArr1 = array [1 .. 5] of real; // One-dimensional array

MyArr2 = array [1 .. 3, 0 .. 2] of integer; // two-dimensional array

Statement:

Var

Arr1: MyArr1;

Yes.

Arr2: array [1 .. 3, 0 .. 2] of integer;

Note: because an array variable corresponds to multiple array elements, the array variables cannot be directly involved in operations or assigned values. Only a single array element can be involved in operations, the operation type depends on the base type of the array type.

B) Dynamic Array:

When defining and declaring a dynamic array, only the array type is specified, and the array size is not specified.

Definition:

Type

<Array type Name> = array of .. array of <base type>

For example:

Type

MyArr4 = array of integer; {one-dimensional dynamic array}

MyArr5 = array of real; {two-dimensional dynamic array}

Or

Arr4: MyArr4;

Arr5: array of char;

Before using dynamic array variables, you must call the standard process SetLength to determine the array size. For example:

SetLength (arr4, 3); {the specified arr4 has 3 elements}

SelLength (arr5, 2, 3); {specify the size of each dimension of arr5 as 2 and the size of the second dimension as 3}

In addition, a special Multidimensional dynamic array can be created. Except the first dimension, the sizes of other dimensions are not fixed.

Var

Arr6: array of integer;

Begin

SetLength (arr6, 2 );

SetLength (arr6 [0], 2 );

SetLength (arr6 [1], 3 );

End;

 

5. Record type

The record here is like a structure of C ++!

Record can be used to combine datasets of different data types and perform operations as a whole.

Record type definition:

Type:

<Record type Name> = Record

<Domain name Table 1>: <type 1>;

<Domain name Table 2>: <type 2>;

....

End;

The <record type Name> is the name of the User-Defined record type. The <domain name table> can contain multiple valid domain name IDs, which are also called field names, <type> it can be any data type. Fields of the same name cannot exist in the same record type. Because of the different scopes, the field names in the record can be the same as those outside the record.

For example:

Type

Student = Record

Id: integer;

Name: string [8];

Sex: char;

End;

Statement:

Var

<Record variable name>: <record type Name>

Record domain access:

<Record variable name>. <field Name>

Use the With statement

With <record variable name> Do <Statement>

<Statement> it can be a simple statement or a compound statement. In a statement, you do not need to specify the record variable name for field access.

With stu2 do

Begin

Id: = 2;

Name: = 'SB ';

Sex: = 'F ';

End;

 

 

6. pointer type:

A Pointer is a special data type. A Pointer variable is called a Pointer variable. A pointer variable has three basic elements of a common variable, namely, the variable name, variable type, and variable value. It differs from a common variable in that: A pointer variable is a variable used to store the memory address of other variables.

Pointer variable declaration:

Type

<Pointer type name >=^ <base type>;

The base type is also an advanced data type, such as the record type.

Statement:

Type

PtrInt = ^ Integer

Var

P1, p2: PtrInt;

Yes.

P4, p5: ^ Integer;

Pointer variable assignment:

Like a common variable, after a pointer variable is declared, the compiler will allocate a memory unit to the pointer variable. However, the value in the memory unit is not yet determined and must be implemented through a value assignment. The format is as follows:

<Pointer variable name >:=@ <identifier>

@ Is The unary operator used to obtain the memory address of the operand. @ can be followed by variables, procedures, functions, and so on.

Var

N: integer;

P: ^ integer;

P: = @ N; // assign a value to the pointer variable

P ^: = 3; // "p ^" indicates the memory unit pointed to by p.

No type pointer variable:

The Declaration format is as follows:

<Pointer variable name>: Pointer;

Type

PInt = ^ integer;

Var

P1: pInt;

M: integer;

N: real;

PAny: Pointer;

Begin

PAny: = @ M;

PAny: = @ N;

P1: = Pint (pAny); // converts the untyped pointer PAny to the integer pointer type p1

P1 ^: Random (n); // you can use p ^ to perform the operation.

End;

For non-type pointer p, you cannot reference the variable it points to in the form of "p ^. To reference a variable without a type pointer, you must convert it to a specified type.

Dynamic usage of pointer variables:

A) dynamic usage of pointer variables:

I) New Process and Dispose Process

If you do not use the @ operator to assign values to pointer variables, pointer variables are called dynamic pointer variables. before accessing dynamic variables, you must first allocate memory units. The standard process New is provided here to allocate memory units for dynamic variables and assign the address of the Unit to the pointer variable. The size of the allocated unit is determined by the type indicated by the pointer, the format of the New call process is as follows:

New (<pointer variable name> );

When the program no longer needs to use dynamic variables, it calls the standard process Dispose to delete the dynamic variables created by New and release the allocated memory units. The process of calling Dispose is as follows:

Dispose (<pointer variable name> );

For example:

Var

P: ^ integer;

Begin

New (p );

P ^ = 10;

Dispose (p );

End;

Ii) GetMem process and FreeMem Process

The standard process GetMem is used to allocate a memory area of the specified size to the dynamic variable and assign the initial address of the area to the pointer variable.

GetMem (<pointer variable name>, <region size> );

If the program no longer needs to use dynamic variables, it will call the standard process FreeMem to delete the dynamic variables created by GetMem and release the allocated memory units. The format of calling FreeMem is as follows:

FreeMem (<pointer variable name> );

The difference between New and GetMem is that New assigns its own address to itself, while GetMem assigns a memory area of the specified size and gives the initial address of this area to him, this address is probably not your own!

Character pointer type:

To be compatible with Windows and C strings, Object Pascal provides a Pchar character pointer type. The pChar pointer is a string pointer pointing to the end of a NULL character.

Var

P: pChar;

Begin

P: = 'delphi 7' ;

End;

 

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/dingdingko/archive/ 2008/04/09 /2267583. aspx

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.