Delphi: Perfect classics-Chapter 5 and chapter 6

Source: Internet
Author: User
Tags modifiers

Chapter 5 Introduction to Common commands

I. TCanvas: This class provides an abstract drawing space for the objects to be mapped. In addition to textures, you can also output text. Tform has the canvas attribute: form1.canvans. textout (100,100, 'abc ');

2. inputbox function: a dialog box that can be input is displayed. Edit1.text: = inputbox ('inputbox example ', 'enter your password:', '007 ');

3. functionmessagedlg (const MSG: string; // information to be displayed

Dlgtype: tmsgdlgtype; // msgdlg type, such as mtconfirmation and mterror. (MT-msgtype)

Buttons: tmsgdlgbuttons; // the buttons to be displayed, such as mbyes and mbno. (MB-msgbutton)

Helpctx: longint): word; // in the dialog box, press the description in F1. Generally 0.

Returned values: mryes, mrok, and mrno. (Mr-msgreturn)

Chapter 6 basic concepts of Delphi and objectpascal

Iii. Data Types and definition Variables

Object Pascal is a strictly typed program language, that is, it makes many differences for data types, so sometimes it is not allowed to replace another data type with one type of data.

Of course, there are also ways to avoid the strictest type. The methods include type conversion, pointer type, variable in record type, and absolute addressing of variables.

1. Declare the collection type:

Type

Workday = (Monday, Wednesday, Thursday, Friday );

There are also cross-platform problems in Delphi: The types are classified into basic types and general types. The basic type is ANSI or Unicode, and the general type is tchar in C ++. Cross-platform, because tchar is related to the platform, it cannot be used.

2. Introduction to non-custom types

1) character types: ansichar and widechar. The common type is Char.

2) Integer type

Basic Integer type:

General Integer type:

3) boolean type:

There are four types: Boolean, bytebool, wordbool, and longbool. Difference: the last three are for cross-platform use. The occupied space is 1, 1, 2, and 4 bytes. In Delphi, Boolean and integer types cannot be automatically converted.

4) real number type:

Basic real number types: rcal48, single, double, extended, comp, and currency. The range and save format of each type are not the same.

Real48: it is compatible with the old Delphi version, but its storage format is not directly supported by Intel series CPUs, so it is slower than other types.

Extended: high precision, but poor portability.

Comp: if possible, use int64 instead.

Currency: a data type with a fixed decimal point. When used with or assigned to a value of another real number type, the value of this type is automatically multiplied or divided by 10000.

Common real number type:

5) string type:

Character string: the string length is 255. Although the maximum value is bytes, it is used to record the length of the first character. Therefore, the minimum value of this type of string is 2 bytes.

Ansistring: A pointer that only occupies 4 bytes of memory space. The value length is limited only by the available memory space.

Widestring: generally used only for COM programming.

6) variant type:

Variant can store values of various types, except record, set, static array, file, Class, Class reference, pointer, and int64. Although various types can be stored, the occupied space is fixed: 16 bytes.

2. Data Types that must be declared with Type

1) Enumeration type

Syntax: Type Enumeration type name = (value 1, value 2 ,......, End value );

For an enumeration type, once declared, any element of this enumeration type cannot be repeatedly declared as one of the other enumeration types.

2) subrange

The value of A subrange is part of a value of a certain type. That is, its value cannot be customized out of thin air. Syntax:

Type subrange type name = start value ...... End value;

3) set type

A set of variables of the same type. Syntax:

Type set type name = set of base type; set type variables can store repeated values.

4) array type: see arrays and pointers in Section 6-6.

5) record type

A set of members of different types. The type of each member must be defined when the record type is declared. Syntax:

Type record type name = record

First member name: type name;

......

Last member name: type name;

End;

4. Define Variables

Naming rules for identifiers:

The length is not limited, but only the first 256 characters are useful.

It consists of English characters, numbers, and underscores, but the first character must be an English character or underscore

Ø space cannot be included

Ø it cannot be a reserved word

A, B, C: integer; Initialization is not allowed during definition.

Define constants, such

Const

Mycount = 34; // Initialization is required when a constant is defined. Sometimes the type can be omitted.

Myname: String = 'color ';

Variable type conversion:

1) automatic conversion: Generally, only conversions of the same type are limited. What is the same type, for example, integer types of the same type include struct int, int64, and byte. Syntax: variable a name: = variable B Name;

2) Forced type conversion: variable a name: = type of variable A (variable B Name );

3) function type conversion: such as inttostr.

5. objectpascal Operators

Arithmetic Operators: +,-, *, and Div (Division, operator. For integers),/(Division, operator), MOD (Division, remainder)

Relational operators: =, <>, <,>, <=,> =

Boolean operators: not, And, or, and XOR (exclusive or exclusive: the return value is true only when the values of the operands on both sides of the left and right are mutually exclusive)

Set OPERATOR: it is used to process the operands of the set type. Some of the Set operators use the same symbols as Relational operators, but they have different functions. The type of the operands must be distinguished during use.

Operator

Function

Supported operand types

Return type (result)

+

Union

Set

Set

-

Difference set

Set

Set

*

Intersection

Set

Set

<=

Included in

Set

Boolean

> =

Include

Set

Boolean

=

Equal

Set

Boolean

<>

Not equal

Set

Boolean

In

Belong

Left ordinal, right set

Boolean

And the Intersection Set must delete repeated elements. In determines whether a number is in a collection.> = Is used to determine whether the value of the left operand contains all the values of the right operand. <= Used to determine whether the value of the left operand is included by the value of the right operand. True is true only when <= and> = are all included. If one is not included, false is returned. = Used to determine whether the values of the left and right operands are completely equal. <> the opposite is true.

Relational operators: =, <>, >=, <=,>, and <in Relational operators can be used to process strings and compare the ASCII code starting with the first character. The string operator has a plus sign (+) but no minus sign -.

Bit logical operators: Not (reverse position), And, Or, XOR, SHL (left shift of bid), and SHR (right shift of bid ). Both SHL and SHR are two-element operators. The variables on the left indicate the shift value, and the variables on the right indicate the number of digits to be moved.

Operator priority:

6. Process Control

1) case ...... Of ...... (Multi-direction Branch)

Case option expression

Option expression 1 (value): Statement 1;

......

Option expression N (value): Statement N;

(Else

Statement ;)

End;

The case expression can be an integer or any other type. Because delphi has a method that can represent the range. Therefore, you can:

Case M1

5. 7:

Begin end;

2, 3, 4:

Begin end;

4. 8, 1:

Begin end;

2) loop statements

① For loop: specify the number of times to be repeated. The interval between the counting variables is fixed. If you want to change the number, you can only control it in the loop body statement.

For counting variable: = start value to end to do // for counting variable: = start value downto end to do

For X: = 1 to 10 do

Begin

Sum: = sum + X;

End;

② While loop statement: it does not explicitly determine the number of repeated executions, but uses conditions to control whether to continue execution.

While Boolean expression do

③ Repeat loop statement: Like a while loop, conditions are used to control whether to continue the loop. The difference is that the loop body is executed first and then the condition is determined. When the condition is true, the loop is stopped.

Repeat

Loop body statement

Until Boolean expression;

3) Labels and jump statements: Label and goto implementation, do not use.

4) Assembly Statement: objectpascal supports the use of Assembly, starting with ASM and ending with end.

7. arrays and pointers

1) array type: a set of continuous data with the same type. The array name represents the address of a memory space.

① An array type can be defined directly in the VaR area. You can also declare an array type in the Type area before defining an array variable or constant in the VaR area. NOTE: For the type area, the type name = array [index type] of base type; For the VaR area, the value is ':'. This feature is not only available for arrays, but also for other types.

② Static array: the size of the static array is determined when the static array is declared.

Declaration of one-dimensional static array: type name = array [index type] of base type; theanwser = array [Boolean] of char; this array has only two elements: theanswer [True] And theanswer [false]. The index type is used to determine the number of array elements. Multidimensional arrays can be defined in two forms: type name = array [index type 1... Index type N] of base type; type name = array [index type] of array [index type] of base type;

③ Dynamic array: the size of the array is determined only when the element value is set to the array. The index type of the dynamic array is integer and starts from 0. The number of dynamic array elements must be set through setlength. Before assigning values to an element, you must set the number of its array elements.

Declaration of one-dimensional dynamic array: type name = array of base type; Declaration of Multidimensional Dynamic Array: type name = array of base type; set size: setlength (array name, one-dimensional size ,... N-dimensional size );

④ Related functions: low and high. Returns the upper and lower limits of the array, that is, the minimum and maximum values of the array index.

2) pointer type

① Declaration and definition: the pointer type name in the Type area = ^ type name; or the pointer type name in the VaR area: ^ type name; if the type name itself is a pointer type, '^' cannot be added; '^' must be added if it is not of the pointer type '.

② Addressing operation (obtain the value of the address indicated by the pointer): Add '^' after the name of the pointer variable, different from C ++.

③ Character pointer: it can be used to operate null-terminated string (with null as the ending sign '#0 ).

Basic Character pointer types: pansichar and pwidechar. Generic character pointer type: pchar. The '+' and '-' operators can be used for these three special pointer types. Addition and subtraction of character pointers is a shift operation.

A: pchar; A: = a + 2;

④ ADDR and PTR functions: ADDR functions the same '@'. PTR is used to convert an address to a pointer. It is useful only when the address value is known.

8. Programs and functions

1) parameter transmission method: pass value, address, and out. Difference: ① When declaring or defining a variable, add a reserved word "Var" in the address transfer mode ". ② When calling a function, the address-passing parameter must be a variable name rather than a value. ③ Out parameter, which is equivalent to reference in C ++. Usage: Add "out" before the parameter name ".

Constant parameter: The value is passed, but the value of the constant parameter cannot be changed within the program. Syntax: Add "const" before the parameter name ".

Array parameters: the function declaration or definition statement cannot contain array indexes. Therefore, for static arrays, you must declare the type in the Type area first, and then use this type to define the function parameters. For dynamic arrays, you can directly declare them without having to be so troublesome. All of these are form parameters, but there is no requirement for real parameters. both dynamic and static Arrays can be used.

2) Declaration modifier: When declaring and defining a function, we can add some declaration modifiers at the end of the header. The modifiers are divided into four types, and a function can use multiple types of modifiers, separated.

① Annotation of the "function call Convention" modifier:

Stdcall: uses the stack method to pass parameters, and the transfer order is left-to-right. This function is used to call Windows API functions.

Cdecl: This modifier is used to pass parameters in the stack mode and the order is left-to-right. This modifier is required when a function is called from a DLL file written in C/C ++.

② Function pre-definition (forward): when a private function is declared in a unit, function calling is restricted by the code sequence because it is not declared in the interface area. This problem can be solved by making a pre-declaration to the function in the implementation area. The method is: Add forward after the function declaration;

③ External: Call functions other than this project. ④ Overload :.

3) Common built-in functions:

Ø mathematical computing:

Ø time and date:

Type conversion:

Ø string processing

Ø Random Number:

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.