Sign in Join
Wiki copytemporary

Variant: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Variant

OldNewLine
1+Back to [[Data type|data types]].
2+
3+A **Variant** data type allows a single variable to store values of multiple types. This allows a statically-typed programming language like Free Pascal some of the flexibility normally found only in a dynamically-typed language like Python.
4+
5+A Variant data type takes more memory than simple data types.The memory requirements of a variant type for 32 bit compilation: 16 bytes or 128 bits. Memory requirements for a variant type in 64 bit compilation: 24 bytes, or 192-bits. Values of any simple data type can be stored in a variant variable. However, operations with the data type Variant are considerably slower than with statically typed simple data type variable.
6+
7+There are two useful functions for working with Variants in the Variants unit:
8+
9+- The **VarType()** function checks which data type the variant holds.
10+- The function **VarToStr()** converts the value of a variable of type Variant to a string representation.
11+
12+Definition of a data field of the data type Variant. It is recommended that you use the [[Variants unit]]:
13+
14+``` pascal
15+Uses
16+ Variants;
17+var
18+ v: Variant;
19+```
20+
21+Example usage:
22+
23+``` pascal
24+uses
25+ Variants;
26+var
27+ v: Variant;
28+ s: string;
29+ i: integer;
30+
31+procedure ProcessVariant(v : Variant);
32+begin
33+
34+ // Example of how to determine the contents of a Variant type:
35+ Case varType(v) of
36+ varEmpty:
37+ Writeln('Empty');
38+ varNull:
39+ Writeln('Null');
40+ varSingle:
41+ Writeln('Datatype: Single');
42+ varDouble:
43+ Writeln('Datatype: Double');
44+ varDecimal:
45+ Writeln('Datatype: Decimal');
46+ varCurrency:
47+ Writeln('Datatype: Currency');
48+ varDate:
49+ Writeln('Datatype: Date');
50+ varOleStr:
51+ Writeln('Datatype: UnicodeString');
52+ varStrArg:
53+ Writeln('Datatype: COM-compatible string');
54+ varString:
55+ Writeln('Datatype: Pointer to a dynamic string');
56+ varDispatch:
57+ Writeln('Datatype: Pointer to an Automation object');
58+ varBoolean:
59+ Writeln('Datatype: Wordbool');
60+ varVariant:
61+ Writeln('Datatype: Variant');
62+ varUnknown:
63+ Writeln('Datatype: unknown');
64+ varShortInt:
65+ Writeln('Datatype: ShortInt');
66+ varSmallint:
67+ Writeln('Datatype: Smallint');
68+ varInteger:
69+ Writeln('Datatype: Integer');
70+ varInt64:
71+ Writeln('Datatype: Int64');
72+ varByte:
73+ Writeln('Datatype: Byte');
74+ varWord:
75+ Writeln('Datatype: Word');
76+ varLongWord:
77+ Writeln('Datatype: LongWord');
78+ varQWord:
79+ Writeln('Datatype: QWord');
80+ varError:
81+ Writeln('ERROR determining variant type');
82+ else
83+ Writeln('Unable to determine variant type');
84+ end;
85+end;
86+
87+begin
88+
89+ // Example 1:
90+ // Value assignment:
91+ v := 1;
92+
93+ //Examples of permissible type assignments, auditions / conversions of data type:
94+
95+ //Explicit data type conversions:
96+ s := VarToStr(v);
97+ Writeln('s:',s);
98+ s := AnsiString(v);
99+ Writeln('s:',s);
100+ i := Integer(v);
101+ Writeln('i:',i);
102+ //Implicit data type conversion:
103+ i := v;
104+ Writeln('i:',i);
105+
106+ ProcessVariant(10);
107+ ProcessVariant(True);
108+ ProcessVariant('Pascal');
109+ ProcessVariant(10.244);
110+
111+end.
112+```
113+
114+Outputs:
115+
116+`s:1`\
117+`s:1`\
118+`i:1`\
119+`i:1`\
120+`Datatype: ShortInt`\
121+`Datatype: Wordbool`\
122+`Datatype: Pointer to a dynamic string`\
123+`Datatype: Double`
124+
125+---
126+*From the Free Pascal wiki, page "Variant", as it stood on 2020-03-03 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history