Several Methods for passing parameters in Delphi Process Functions

Source: Internet
Author: User

Reproduced to: http://www.qqread.com/delphi/q479333.html

The modifier Const, Var, and Out are passed in the Delphi Process and function. Another parameter without modifier is passed by default by value.

1. parameters are passed as values by default.

Procedure TForm1.ProcNormal (Value: string );
Begin
OrigNum: = Value + 'me ';
LblReturn. Caption: = OrigNum; // The value of OrigNum is 'Hello me'
LblOrig. Caption: = Value; // The Value is 'Hello'
End;

Call:

OrigNum: = 'hello ';
ProcNormal (OrigNum );

2. Pass the parameter in Const mode. This parameter cannot be changed during the call process and will be optimized by the compiler. We recommend that you use this method as much as possible.

Procedure TForm1.ProcConst (const Value: string );
Begin
OrigNum: = Value + 'me ';
LblReturn. Caption: = OrigNum; // The value is 'Hello me'
LblOrig. Caption: = Value; // It is 'Hello me'
End;

3. PASS Parameters by reference

Procedure TForm1.ProcRef (var value: string );
Begin
OrigNum: = Value + 'me ';
LblReturn. Caption: = OrigNum; // The value is 'Hello me'
LblOrig. Caption: = Value; // It is 'Hello me'
End;

4. Pass the parameter in the Out mode. When passing the parameter in this mode, the parameter can be ignored even if there is a value, which is generally used for output, it can return multiple values in a process. We usually use it in a distributed object model, such as COM.

Procedure TForm1.ProcOut (out Value: string );
Begin
OrigNum: = Value + 'me ';
LblReturn. Caption: = OrigNum; // It is 'me'
LblOrig. Caption: = Value; // It is 'me'
End;

5. There is no type parameter. This is a special method. The type of the parameter is uncertain and can only be modified using Const, Var, or Out. It cannot be used to pass parameters in the value mode. The example is as follows:

Procedure TForm1.ProcUntype (const Value );
Begin
OrigNum: = string (Value) + 'me ';
LblReturn. Caption: = OrigNum; // The value is 'Hello me'
Lblorig. Caption: = string (value); // It is 'Hello me'
End;

6. default parameter. If this parameter is not provided during the call, the default value is used.

Procedure tform1.procdefault (const value, const defaultvalue: String = '20140901 ');
Begin
Orignum: = value + 'me' + defaultvalue;
Lblreturn. Caption: = orignum; // The value is 'Hello me 123'
Lblorig. Caption: = value; // The value is 'Hello me 123'
End;

7. Open array parameters, that is, the number of elements in the parameter array is uncertain.

Procedure tform1.procarray (const value: array of string );
VaR
I: integer;
Begin
For I: = low (value) to high (value) Do
Orignum: = orignum + value [I]; // after the call, the value is 'Hello ABC dbd'
Lblreturn. Caption: = orignum;
End;

Call:

Orignum: = 'hello ';
ProcArray (['abc', 'dbd']);

8. Non-type open array parameters, that is, the number of types and elements are not sure. On the WIN32 platform, the type of this parameter is actually array of TVarRec. The example is as follows:

Procedure TForm1.ProcArrayConst (const Value: array of const );
Var
I: Integer;
Begin
For I: = Low (Value) to High (Value) do
With Value [I] do
Case VType
VtAnsiString: OrigNum: = OrigNum + String (VAnsiString );
VtInteger: OrigNum: = OrigNum + IntToStr (VInteger );
VtBoolean: OrigNum: = OrigNum + BoolToStr (VBoolean );
VtChar: OrigNum: = OrigNum + VChar;
VtExtended: OrigNum: = OrigNum + FloatToStr (VExtended ^ );
VtString: OrigNum: = OrigNum + VString ^;
VtPChar: OrigNum: = OrigNum + VPChar;
VtObject: OrigNum: = OrigNum + VObject. ClassName;
Vtclass: orignum: = orignum + vclass. classname;
Vtcurrency: orignum: = orignum + currtostr (vcurrency ^ );
Vtvariant: orignum: = orignum + String (vvariant ^ );
Vtint64: orignum: = orignum + inttostr (vint64 ^ );
End;
Lblreturn. Caption: = orignum; // after the call, it is 'Hello ABC 3'
End;

Call:

Orignum: = 'hello ';
Procarrayconst (['abc', 3]);

These are common methods for passing parameters.

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.