Delphi keyword explanation 1

Source: Internet
Author: User

This document consists
Orange

Original, copyright belongs to orange

Absolute
// It allows you to create a new variable, and the starting address of the variable is the same as that of another variable.
VaR
STR: String [32];
Strlen: byte absolute STR;

// This declaration specifies that the start address of the variable strlen is the same as that of Str.
// Because the length of the string is saved at the 0th position of the string, the strlen value is the length of the string.
Begin
STR: = 'abc ';
Edit1.text: = inttostr (strlen );
End;

 

Abstract
// It allows you to create abstract methods, including classes with abstract methods called abstract classes.
// The abstract keyword must be used together with the virtual or dynamic keyword, because the abstract method must be overwritten.
// The abstract class cannot be instantiated, and the abstract method cannot contain the method body.
Type
Tdemo = Class
Private
Protected
Procedure X; virtual; abstract;
Public
Constructor create;
Destructor destroy; override;
Published
End;

 

And
// 1. Representation logic and
If (a> 0) and (B> 0) then

// 2. bitwise operation
VaR
A, B, C: integer;
Begin
C: = (A and B );
End;

// When and is used to represent the logic, the expression between and must be enclosed in parentheses to avoid conflict with conditions.
// Example:
If a> 0 and B> 0 then
// The Compiler may understand this as follows:
If a> (0 and B)> 0 then
// Or:
If (a> 0) and (B> 0) then
// But during actual compilation, the compiler will generate a conflict and report an error.
// And the first form may contain A> B> C, which is not supported in Delphi.
// Brackets must be used when the and operator is used to distinguish between left and right conditions.
// Brackets must be added for bitwise operations to enclose and left and right parameters.

 

Array
// Array indicates an array. Any object can be declared as an array. The array can be static or dynamic.

// Static Array
VaR
Arr1: array [1 .. 10] of integer;

// Dynamic array. because the number of elements is unknown during declaration, you must use the setlength method to set the array size later.
VaR
Arr2: array of integer;

// When an array is used as a parameter, the size of the array cannot be input. Only the array name can be input, and the number of elements of the array can be obtained using the length method.
Function x (A: array of integer): integer;
VaR
I: integer;
Begin
Result: = 0;
For I: = 0 to length (a)-1 do
Result: = Result + A [I];
End;

 

As
// As is used to convert an object to another object
Procedure btnclick (Sender: tobject );
Begin
(Sender as tbutton). Caption: = 'clicked ';
End;

// For object filling interface conversion, the as must be used
(Httprio as iexp). getconnection;

// As cannot be used for data type conversion.CodeIs incorrect:
VaR
I: integer;
S: string;
Begin
S: = (I as string );
End;
// The correct statement is as follows:
S: = string (I );

 

ASM
// The ASM keyword is used to insert assembly code. When using assembly code, you must use the structure of ASM... end; instead of begin... end;
Function inttohex (value: integer; digits: integer): string;
ASM
CMP edX, 32
Jbe @ A1
XOR edX, EDX
@ A1: Push ESI
MoV ESI, ESP
Sub ESP, 32
Push ECx
MoV ECx, 16
Call cvtint
MoV edX, ESI
Pop eax
Call system. @ lstrfrompcharlen
Add ESP, 32
Pop ESI
End;

 

Cycler
// The author er keyword is used to support early compilation, such as 80386.
// What is the difference between it and ASM: ASM allows Win32 assembly, while worker er only allows 80x86 assembly, which does not allow the appearance of invoke statements.
Function inttohex (avalue: int64): string; Cycler;

 

Automatic
// The ed access Delimiter is used to describe an automatically typed member.Program.
// Members in the comobj unit and Their instances cannot use the automatic access identifier.
Type
Tdemo = Class
Automatic
STR: widestring;
End;

// In the next version of the program, modify STR
Type
Tdemo = Class
Automatic
STR: ansistring;
End
// The STR variable of the new version can accept the widestring data of the old version and be automatically converted to ansistring.
// In actual development, if there is no special need, there is generally no need for an automatic access identifier.

 

Begin
// The begin keyword is used to indicate the start of a program or a structure. The end keyword must be used to end the process.
Procedure X;
Begin
Showmessage ('a demo ');
End;

// General structure, such as if, for, and while, you also need to use the begin keyword to mark the start point of the structure.
For I: = 1 to 100 do
Begin
Sum: = sum + I;
If sum> 1000 then break;
End;

 

Case
// The case statement is used to select conditions. The selected objects of the case statement must be ordered, including integer, enumeration, and case.
// The case statement must end with end. If there are no matching options, you can add Else To make a general choice.
Function getdays (ayear, amonth: integer): integer;
Begin
Case amonth
1, 3, 5, 7, 8, 10, 12: Result: = 31;
4, 6, 9, 11: Result: = 30;
2: Begin
If isleapyear (ayear) then
Result: = 29
Else
Result: = 28;
End;
Else
Result: = 0;
End;

 

Cdecl
// Cdecl is a function call protocol, which specifies the rules that must be followed for calling a function from a DLL written in C or C ++.
// It can convert the Data Type in C or C ++ to Delphi.
// For example, the Code in C ++:
Int X (int I)
{
Return I * 2;
}
// This function is compiled in demo. dll and must be used for calling using Delphi:
Function x (I: integer): integer; cdecl; External 'demo. dll ';

 

Class
// The class keyword is used to declare or inherit a class. It can also make the class and interface inherit at the same time.
// In addition, the class keyword can also be used to declare a common class method, so that the parent class can access the subclass method from the class.
Type
Classdemo = Class (tobject)
Private
Public
Constructor create;
End;

// If the class declaration method is used, this method can be used in both classes and related classes, for example:
Type
Classa = Class
Private
Public
Procedure y;
End;

Type
Classb = Class (classa)
Private
Public
Class procedure X;
End;
// When used, classa can directly access the X method of classb
Procedure classa. Y;
Begin
Self. X;
End;
// At this time, the parent class calls the class method of the subclass as its own method.

 

Const
// The const keyword is used to declare constants. Data declared using const cannot be changed in the program.
// It can also be used to declare function parameters. Parameters specified by const cannot be changed in the function.
Const myfilename = 'delphi ';
Const myinteger = 100;

// If you declare a constant with const, you do not need to specify its data type. The system automatically determines the type and adjusts it.
// You can use const to declare unchangeable parameters in the function.
Function x (const I: integer): string;
// The value of I cannot be changed during the function operation.

 

Constructor
// The constructor keyword is used to declare the constructor of A Class. When the class is instantiated, this function is called first.
// The constructor is generally represented by create. The create method can be associated with the createwnd method in the class.
Type
Classdemo = Class (tobject)
Private
Fvalue: integer;
Public
Constructor create;
End;

Constructor classdemo. Create;
Begin
Fvalue: = 0;
End;

 

Contains
// The contains keyword indicates whether a package contains a file.
// The file imported with contains must be added to the package file, which can avoid the loss of references to key files.
Package datax;
Requires
RTL, clx;
Contains
DB, dblocal, dbxpress;
End.

 

Default
// The default keyword is used to indicate the default value of an attribute.
// Only ordered attributes allow the existence of default values; otherwise, the attribute values must be initialized in the constructor.
Type
Classdemo = Class
Private
Fvalue: integer;
Published
Property Value: integer read fvalue write fvalue default 0;
End;

// It can also indicate the default attributes of a class.
Property strings [index: integer]: String read getstring write putstring; default;

 

Destructor
// Destructor is used to identify the destructor, which is automatically called when the class is released.
// The Destructor can only be overwritten and cannot be overloaded. Destroy is usually used as the function name.
Type
Classdemo = Class (tcomponent)
Public
Destructor destroy; override;
End;

// Rewrite the destroy method in the tcomponent class.
// However, if you want to reload the destructor, it is not allowed. The following code is incorrect:
Destructor destroy; overload;

 

Dispid
// The dispid keyword is used in the dispinterface interface to specify a specific adaptation sequence number.
// In the dispinterface interface, the adaptation sequence number must be unique,
// If dispid is not specified, the system automatically assigns an adaptation sequence number to each method in the interface.
// You can access the methods in the dispinterface interface by using an adaptation serial number.
Type
Istringsdisp = dispinterface
['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}']
Property controldefault [index: integer]: olevariant dispid 0; default;
Function count: integer; dispid 1;
Property item [index: integer]: olevariant dispid 2;
Procedure remove (Index: integer); dispid 3;
Procedure clear; dispid 4;
Function add (item: olevariant): integer; dispid 5;
Function _ newenum: iunknown; dispid-4;
End;

 

Dispinterface
// Dispinterface is used to declare a specific adapter interface, which can accept transmitted data from the standard system interface.
// The interface declared with dispinterface cannot be inherited but can only be referenced.
// Methods in dispinterface can only be called and must be dynamically bound.
// You can use dispid to allocate an adaptation sequence number for the contact.
// Dispinterface can only be used on Windows. If it is developed in Linux, this keyword is automatically blocked by the system.
// Generally, dispinterface is not used.

// For instance details, see dispid

 

Div
// Div is used to calculate the integer quotient of two numbers. The two values used for div calculation must be integer, and the calculation result is also integer.
VaR
A, B, C: integer;
Begin
A: = 20; B: = 3;
C: = a div B; {6}
End;

 

Do
// The do keyword is used for the For, while, on, and with statements to form a specific structure.

// For statement:
For I: = 1 to 100 do sum: = sum + I;

// While statement:
While I & lt; 100 do
Begin
Sum: = sum + I;
INC (I );
End;

// On statement (Exception Handling ):
Try
I: = strtoint (s );
Except
On exception do showmessage ('error! ');
End;

// With statement:
With memo1.lines do
Begin
Clear;
Append ('abc ');
Append ('20140901 ');
End;

 

Downto
// The downto keyword is used for the for statement, indicating that the cyclic variable is decreasing.
For I: = 100 downto 1 do
Listbox1.items. Add (inttostr (I ));

// In the for statement, the "to" keyword is used for variable increments and the "downto" keyword is used for variable increments.

 

Dynamic
// Dynamic is used to declare a dynamic method,
// Dynamic methods can be overwritten and the code size can be minimized (different from virtual ).
Procedure X (I: integer); dynamic;

 

Else
// Else is used to guide the running direction of the program. It can be used with if, case, and on statements. If the conditions are not met, the program is transferred to else for running.

// If statement (in the IF statement, a semicolon is not allowed before else ):
If A> B then
C: =
Else
C: = B;

// Case statement:
Case tag
1: Result: = 1;
2: Result: = 2;
3: Result: = 3;
Else
Result: = 0;
End;

// On statement (Exception Handling ):
Try
I: = strtoint (s );
Excpet
On ezerodivide do result: = 1;
On eoverflow do result: = 2;
Else
Result: = 0;
End;

 

End
// End is used to end a statement block or a unit.
// It can match with begin, Case, class, interface, ASM, unit, package, etc.
// For statement blocks (partial termination), you must add a semicolon after end.
// For the unit or package (Global end), the end must be added after the end.
// You cannot add symbols after the end before the else keyword in the IF statement.
Procedure X;
Begin
With button1 do
Begin
If button1.showhint then
Button1.caption: = 'hinted'
Else
Button1.caption: = 'not hinted ';
End;
End;

// End with end in the package:
Package datax;
Requires
RTL,
Clx;
Contains dB, dblocal, dbxpress;
End.

 

Except
// The keyword "failed T" is used for exception handling. It must be used in a try statement. If an exception occurs, the statement after "failed T" is executed.
Try
I: = strtoint (s );
Except
Showmessage ('error! ');
End;

 

Export
// Export indicates the function call protocol, indicating that the function can be output and the output function can be called locally or remotely.
// Other programs can call functions in the program in the form of DLL. It is backward compatible.
Function add (A, B: integer): integer; export;

// If this program is compiled into demo.exe and another program needs to call this function, you can use the following statement
Function add (A, B: integer): integer; stdcall; External 'demo.exe ';

 

Exports
// Exports is used to output objects. It must be used between interfaces and implementations and multiple items can be output at the same time. items and items are separated by commas.
Library Demo;

Function x (I: integer): string; stdcall;
Begin
Result: = inttostr (I );
End;

Exports
X;

Begin
End.

// If the output object is overloaded, an alias must be given to the object, with parameters specified.
Library Demo;

Function x (I: integer): string; overload; stdcall;
Begin
Result: = inttostr (I );
End;

Function x (s: string): integer; overload; stdcall;
Begin
Result: = strtoint (s );
End;

Exports
X (I: integer) Name 'x1 ',
X (S: string) Name 'x2 ';

Begin
End.

 

External
// The external keyword is used to reference an external or OBJ method.
{$ L demo. OBJ}
Procedure X (I: integer); external;

// If it is referenced from a DLL or an external program, you can use the following code:
Function A (filename: string): string; External 'demo. dll ';

// If the referenced function is overloaded, the referenced name must be specified.
Function A (name: string): string; overload; stdcall; External 'demo. dll 'name 'a1 ';
Function A (Code: integer): string; overload; stdcall; External 'demo. dll 'name' A2 ';

// Note the case when using the external keyword. Otherwise, an error occurs.

 

Far
// Far indicates the function call protocol, indicating that the function can be called remotely.
// Other programs can call functions in the program in the form of DLL. It is backward compatible.
Function add (A, B: integer): integer; far;

// If this program is compiled into demo.exe and another program on another computer needs to call this function, you can use the following statement:
Function add (A, B: integer): integer; stdcall; External 'demo.exe ';

 

File
// The file keyword indicates the file operation type. The file must be declared as file,
// If the object type is appended to the file, the object can be defined as a read/write operation for the specified type of data.
Type
Tperson = record
Pname: String [32];
Page: integer;
End;
VaR
Pfile: file of tperson;

 

Finalization
// The finalization keyword identifies the method to be called when a unit is released,
// It is usually used to release objects that cannot be automatically released in a unit.
// The most common condition of finalization is to perform reverse Initialization on the OLE object.
Initialization
ActiveX. oleinitialize (NiL );
Finalization
ActiveX. oleuninitialize;

 

Finally
// The finally keyword indicates the method that must be called during exception handling,
// Whether or not an exception occurs, the finally statement is always executed at the end of the try statement.
Try
Node: = node. getnext;
Edit1.text: = node. text;
Finally
Node: = nil;
End;

 

For
// The for keyword leads to the for loop structure, which is used for repeating a specified number of times.
For I: = 1 to 100 do sum: = sum + I;

// If the cyclic variable is decreasing, you can use the downto keyword.
For I: = 100 downto 1 do Inc (SUM );

 

Forward
// The forward keyword is used for the pre-Definition of the method. Only the method declaration is defined, and the method is implemented after the program.
// This is conducive to the readability of the code. You can put all the declarations together and then put all the implementations together.
Function x (I: integer): integer; forward;
Procedure y (S: string); forward;
...
Function x;
Begin
Result: = I * 2;
End;

Procedure y;
Begin
Writeln (s );
End;

// You do not need to enter the parameters and return values of the method when implementing the method with the forward pre-declaration. You can directly use the method name.

 

Function
// Function is used to declare a function.
Function x (I: integer): integer;

// It can also be used for dynamic function declaration
Type
Tfun = function (I: integer): integer of object;

// During dynamic declaration, you do not need to specify the function name. You only need to specify the parameter and return type. The specific function name can be bound later.

 

Goto
// The GOTO statement is used to jump to any position in the current structure layer.
// The line number must be declared with the label keyword at the declaration.
// Since the GOTO statement will destroy the program structure, it is not recommended to use it.
VaR
A, B: integer;
Label
X, Y;
Begin
If A> B then
Goto x
Else
Goto y;
X:
Writeln ('a> B ');
Y:
Writeln ('B> ');
End;

 

If
// The if keyword is used to determine the if condition.
VaR
A, B: integer;
Begin
A: = 2; B: = 3;
If A> B then
Writeln ('a = '+ inttostr ())
Else
Writeln ('B =' + inttostr (B ));
End;

// The normal structure of the IF statement is if... then... else, or else.
// If there are multiple substatements in the IF statement, you must use the begin... end structure to differentiate them.
If A> B then
Begin
Writeln ('a> B ');
Writeln ('a = '+ inttostr ());
Writeln ('B =' + inttostr (B ));
End
Else
Writeln ('B> ');

 

Implementation
// Implementation identifies the implementation part of the unit. The basic structure of the Unit is as follows:
// Unit... interface... implementation... end.
// The function body and process body must be written after the implementation keyword.
// If an object is referenced after implementation, the object is not public and can only be used by the Unit itself.
Implementation
Uses frmabout;
Begin
Formabout. show;
End;

// A complete unit must have the implementation part.

 

implements
 // implements indicates that an attribute is inherited from an interface, and is converted to an interface object. 
// dynamically bind a property through the interface and set the property value dynamically.
type
imyinterface = interface
procedure P1;
procedure P2;
end;
tmyimplclass = Class
procedure P1;
procedure P2;
end;
tmyclass = Class (tinterfacedobject, imyinterface)
fmyimplclass: tmyimplclass;
property myimplclass: tmyimplclass read fmyimplclass implements imyinterface;
procedure imyinterface. p1 = myp1;
procedure myp1;
end;
// After the implements declaration, you can specify the object of the method in the interface during the class declaration, in the preceding example:
procedure imyinterface. p1 = myp1;

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.