Sign in Join
Wiki copytemporary

Variant: what changed

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

OldNewLine
11Back to [[Data type|data types]].
22
33A **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.
44
55A 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.
66
77There are two useful functions for working with Variants in the Variants unit:
88
99- The **VarType()** function checks which data type the variant holds.
1010- The function **VarToStr()** converts the value of a variable of type Variant to a string representation.
1111
1212Definition of a data field of the data type Variant. It is recommended that you use the [[Variants unit]]:
1313
1414``` pascal
1515Uses
1616 Variants;
1717var
1818 v: Variant;
1919```
2020
2121Example usage:
2222
2323``` pascal
2424uses
2525 Variants;
2626var
2727 v: Variant;
2828 s: string;
2929 i: integer;
3030
3131procedure ProcessVariant(v : Variant);
3232begin
3333
3434 // Example of how to determine the contents of a Variant type:
3535 Case varType(v) of
3636 varEmpty:
3737 Writeln('Empty');
3838 varNull:
3939 Writeln('Null');
4040 varSingle:
4141 Writeln('Datatype: Single');
4242 varDouble:
4343 Writeln('Datatype: Double');
4444 varDecimal:
4545 Writeln('Datatype: Decimal');
4646 varCurrency:
4747 Writeln('Datatype: Currency');
4848 varDate:
4949 Writeln('Datatype: Date');
5050 varOleStr:
5151 Writeln('Datatype: UnicodeString');
5252 varStrArg:
5353 Writeln('Datatype: COM-compatible string');
5454 varString:
5555 Writeln('Datatype: Pointer to a dynamic string');
5656 varDispatch:
5757 Writeln('Datatype: Pointer to an Automation object');
5858 varBoolean:
5959 Writeln('Datatype: Wordbool');
6060 varVariant:
6161 Writeln('Datatype: Variant');
6262 varUnknown:
6363 Writeln('Datatype: unknown');
6464 varShortInt:
6565 Writeln('Datatype: ShortInt');
6666 varSmallint:
6767 Writeln('Datatype: Smallint');
6868 varInteger:
6969 Writeln('Datatype: Integer');
7070 varInt64:
7171 Writeln('Datatype: Int64');
7272 varByte:
7373 Writeln('Datatype: Byte');
7474 varWord:
7575 Writeln('Datatype: Word');
7676 varLongWord:
7777 Writeln('Datatype: LongWord');
7878 varQWord:
7979 Writeln('Datatype: QWord');
8080 varError:
8181 Writeln('ERROR determining variant type');
8282 else
8383 Writeln('Unable to determine variant type');
8484 end;
8585end;
8686
8787begin
8888
8989 // Example 1:
9090 // Value assignment:
9191 v := 1;
9292
9393 //Examples of permissible type assignments, auditions / conversions of data type:
9494
9595 //Explicit data type conversions:
9696 s := VarToStr(v);
9797 Writeln('s:',s);
9898 s := AnsiString(v);
9999 Writeln('s:',s);
100100 i := Integer(v);
101101 Writeln('i:',i);
102102 //Implicit data type conversion:
103103 i := v;
104104 Writeln('i:',i);
105105
106106 ProcessVariant(10);
107107 ProcessVariant(True);
108108 ProcessVariant('Pascal');
109109 ProcessVariant(10.244);
110110
111111end.
112112```
113113
114114Outputs:
115115
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`
116+```text
117+s:1
118+s:1
119+i:1
120+i:1
121+Datatype: ShortInt
122+Datatype: Wordbool
123+Datatype: Pointer to a dynamic string
124+Datatype: Double
125+```
124126
125127---
126128*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