Go Introduction to the Delphi Variant type (variant) (Flow vs. Variant types, functions commonly used for variant types)

Source: Internet
Author: User

Introduction to the Delphi Variant type (variant) (Flow vs. Variant types, functions commonly used for variant types)first, the variant types commonly used functions are described:

Variant: One can have a variety of data types, you can also tell what type of data is currently stored (by using the VarType function), you can assign different data types to the same variant, as long as the variant contains numeric values to execute the algorithm; The variant array is nothing more than a variant array and does not have to contain the same type of data;

1, vararrayof function: More quickly create a one-dimensional variant array, you can create an entirely different array of values;
function vararrayof (const Values:array of Variant): Variant;

Vararrayof is a way to generate a one-dimensional variant of a fast (writing code quickly rather than running fast), which accepts a variant of the open array, using this data to generate a one-dimensional variant array of Variant (variant type). Because Delphi supports the open array type parameter, you can generate an array with a single line of statements. However, it has two drawbacks: only one-dimensional arrays can be generated, and only an array of variant types is generated. Using a Variant array is similar to using a standard Delphi array;
For example: MyArray: = vararrayof ([' Levi ', ' ['] ', ' 60.369 ', ' China ');

2. Vararraycreate function:
function vararraycreate (const bounds:array of Integer; Vartype:tvartype): Variant;

Bounds: Tells the upper and lower bounds of the array; VarType: Determines what type of data is stored in the array. Other arrays (such as two-dimensional, or other types) can only be created with vararraycreate and are assigned values in a loop.
For example: Create an array of arrays that can mimic any type of data structure:
Vararrayx: = Vararraycreate ([1,10], varvariant);
A single element of an array can mount an array: vararrayx[1]: = Vararraycreate ([1,5], varvariant);

3, Vararrayhighbound, Vararraylowbound function: Returns the upper/lower bounds of the variant array, starting with 1 (1: column; 2: row; 3:3-dimensional last dimension).
function Vararrayhighbound (const a:variant; Dim:integer): Integer;

4. Vararrayredim function: Modifies the maximum limit of the variant array.
Procedure Vararrayredim (a:variant; Highbound:integer);

5. Vararraydimcount function: Returns the Variant array dimension
Unction vararraydimcount (const a:variant): Integer;

6, Vararraylock, Vararrayunlock function: Avoid running time check. Typically used to initialize a large array.
function Vararraylock (const a:variant): Pointer;
Procedure Vararrayunlock (var a:variant);

When each element of a Variant array is assigned, the runtime logic is checked to determine the compatibility of the data type, the location of each element, and so on. To avoid run-time checks, use the Vararraylock () function and the Vararrayunlock () procedure. The Vararraylock () function locks an array in memory so that the array is no longer moved and resized, and can return a pointer to the array data. The Vararrayunlock () procedure is used to unlock an array that is locked by the Vararraylock () function so that the array can be re-moved or resized. After you lock an array, you can initialize the arrays in a more efficient way.

7. Vararrayref function: Get the data that the variant points to
function Vararrayref (const a:variant): Variant;

8, Varisarray function: is a simple boolean check function, determine whether it is a Variant array, is the return true
function Varisarray (const a:variant): Boolean; overload;
function Varisarray (const a:variant; Aresolvebyref:boolean): Boolean; overload;

9. Other functions

The 1>, varclear () procedure clears the variant variable and sets the value of the VType field to Varempty.
2>, Varcopy () copies the source to Dest.
3>, Varcast () converts a variant to the specified type and stores it in another variant variable.
4>, VarType () returns the Varxxx type code for the specified variant.
The functions of 5>, Varastype () and Varcast () are the same.
6>, Varisempty () returns True if the type code of a Variant variable is varempty.
7>, Varisnull () determines whether a Variant variable contains a null value.
8>, Vartostr () converts a Variant variable to a string expression (an empty string if the variant is Varempty or varnull).
9>, Varfromdatetime () returns a Variant variable that holds the value of the specified tdatetime type.
10>, Vartodatetime () returns the value of the Tdatetime type in the variant.

Second Simple instance code:

Var

A, b:variant;

I, J, K, M:integer;

Begin

A is a one-dimensional variant array, the element type is variant, the number of elements is 3, the upper bound is 2, the lower bound is 0.

A: = vararrayof ([1234, ' abc ', Null]);

B is a two-dimensional array of 16 rows and 4 columns

B: = Vararraycreate[0, 3, 1, +], varolestr);

I: = Vararrayhighbound (b, 1); I is the largest column number of B: 3;

J: = Vararraylowbound (b, 1); J is the smallest column number of B: 0;

K: = Vararrayhighbound (b, 2); K is the maximum line number of B: 16;

M: = Vararraylowbound (b, 2); M is the smallest line number of B: 1.

End

Note that the Variant array subscript is listed before the row, the element type is Pwidechar, the maximum column number is 3, the minimum column number is 0, the maximum line number is 16, and the minimum line number is 1.

Iii. Conversion of variant types and flows

1. The variant type turns into a stream

Procedure Varianttostream (const data:olevariant; Stream:tstream);

Var

P:pointer;

Begin

P: = Vararraylock (Data); Locks and returns the data that a pointer points to. Avoid run time checks

Try

Stream.Write (p^, Vararrayhighbound (data,1) + 1); Returns the maximum number of columns

Finally

Vararrayunlock (Data); Unlocks so that the array can be re-moved or resized.

End

End

2. Flow into Variant type

function Streamtovariant (stream:tstream): olevariant;

Var

P:pointer;

Begin

Result: = Vararraycreate ([0, Stream.size-1], varbyte);//Create a thought array. Type is Varbyte

P: = Vararraylock (Result);

Try

Stream.position: = 0; Set the position of the stream

Stream.read (p^, stream.size);

Finally

Vararrayunlock (Result);

End

End

Go Introduction to the Delphi Variant type (variant) (Flow vs. Variant types, functions commonly used for variant types)

Related Article

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.