Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə104/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   100   101   102   103   104   105   106   107   ...   294
DelphiLanguageGuide

Variant Types
This topic discusses the use of variant data types.
Variants Overview
Sometimes it is necessary to manipulate data whose type varies or cannot be determined at compile time. In these
cases, one option is to use variables and parameters of type Variant, which represent values that can change type
at runtime. Variants offer greater flexibility but consume more memory than regular variables, and operations on
them are slower than on statically bound types. Moreover, illicit operations on variants often result in runtime errors,
where similar mistakes with regular variables would have been caught at compile time. You can also create custom
variant types.
By default, Variants can hold values of any type except records, sets, static arrays, files, classes, class references,
and pointers. In other words, variants can hold anything but structured types and pointers. They can hold interfaces,
whose methods and properties can be accessed through them. (See Object interfaces.) They can hold dynamic
arrays, and they can hold a special kind of static array called a variant array. (See Variant arrays.) Variants can mix
with other variants and with integer, real, string, and Boolean values in expressions and assignments; the compiler
automatically performs type conversions.
Variants that contain strings cannot be indexed. That is, if 
V
 is a variant that holds a string value, the construction
V[1]
 causes a runtime error.
You can define custom Variants that extend the Variant type to hold arbitrary values. For example, you can define
a Variant string type that allows indexing or that holds a particular class reference, record type, or static array. Custom
Variant types are defined by creating descendants to the 
TCustomVariantType
 class.
Note:
This, and almost all variant functionality, is implemented in the 
Variants
 unit.
A variant occupies 16 bytes of memory and consists of a type code and a value, or pointer to a value, of the type
specified by the code. All variants are initialized on creation to the special value Unassigned. The special value
Null
 indicates unknown or missing data.
The standard function 
VarType
 returns a variant's type code. The 
varTypeMask
 constant is a bit mask used to
extract the code from 
VarType
's return value, so that, for example,
VarType(V) and varTypeMask = varDouble
returns True if 
V
 contains a Double or an array of Double. (The mask simply hides the first bit, which indicates whether
the variant holds an array.) The TVarData record type defined in the 
System
 unit can be used to typecast variants
and gain access to their internal representation.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   100   101   102   103   104   105   106   107   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin