Array of const

Source: Internet
Author: User
  1. {A common error when working with pchars is to store a local variable in a data structure, or return it as a value. when your routine ends, the pchar disappears because it is a pointer to memory, and not a reference counted copy of the string. for example :}
  2. Function title (N: integer): pchar;
  3. VaR
  4. S: string;
  5. Begin
  6. S: = format ('title-% d', [N]);
  7. Result: = pchar (s); // don't do this
  8. End;
  9. {This example returns a pointer to string data that is freed when the title function returns .}
  10. Procedure tform1.mydataafterdelete (Dataset: tdataset );
  11. Begin
  12. Statusbar1.simpletext: = format ('there are now % d records in the table', [dataset. recordcount]);
  13. End;

The prototype of the format function is Function Format (const format: string; const ARGs: array of const): string; overload;

The second parameter is of the array of const type. If you do not know the type, search for it:

 

Variant open array parameters


The variant development array parameter allows passing arrays of different types of expressions to a single process or function. To define a routine that contains variant open array parameters, you must specify array of const as the parameter type. For example,

Procedure dosomething (A: array of const );

Here we declare a process called DoSomething, which can operate on different types of arrays.

The array of const structure is equivalent to array of TVarRec. TVarRec, declared in the unit System, is used to represent a record, the record contains a variant that can store multiple values (integer, Boolean, character, real number, String, pointer, Class, Class reference, interface, variant, etc. The VType field in TVarRec indicates the type of each element in the array. Some types are passed as pointers without passing values. Especially long strings are passed as pointers and must be converted to the string type.

The following example uses the variant open array parameter in the function. This function creates a string representation for each element that is accepted and is connected to a string. The string processing routines called in this function are defined in the SysUtils unit.

Function MakeStr (const Args: array of const): string;

Const

BoolChars: array [Boolean] of Char = ('F', 'T ');

Var

I: Integer;

Begin

Result: = '';

For I: = 0 to High (Args) do

With Args [I] do

Case VType

VtInteger: Result: = Result + IntToStr (VInteger );

Vtboolean: Result: = Result + boolchars [vboolean];

Vtchar: Result: = Result + vchar;

Vtextended: Result: = Result + floattostr (vextended ^ );

Vtstring: Result: = Result + vstring ^;

VtPChar: Result: = Result + VPChar;

VtObject: Result: = Result + VObject. ClassName;

VtClass: Result: = Result + VClass. ClassName;

VtAnsiString: Result: = Result + string (VAnsiString );

Vtcurrency: Result: = Result + currtostr (vcurrency ^ );

Vtvariant: Result: = Result + String (vvariant ^ );

Vtint64: Result: = Result + inttostr (vint64 ^ );

End;

End;

You can use an open array Constructor (see open array constructor) to call this function. For example:

Makestr (['test', 100, '', true, 3.14159, tform])

The above call will return the string "test100 t3.14159tform ".

That is to say, the [] symbol can be used to enclose any variables of the format type directly in the format.

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.