Variant: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | Back to [[Data type|data types]]. | |
| 2 | 2 | ||
| 3 | 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 | 4 | ||
| 5 | 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 | 6 | ||
| 7 | 7 | There are two useful functions for working with Variants in the Variants unit: | |
| 8 | 8 | ||
| 9 | 9 | - The **VarType()** function checks which data type the variant holds. | |
| 10 | 10 | - The function **VarToStr()** converts the value of a variable of type Variant to a string representation. | |
| 11 | 11 | ||
| 12 | 12 | Definition of a data field of the data type Variant. It is recommended that you use the [[Variants unit]]: | |
| 13 | 13 | ||
| 14 | 14 | ``` pascal | |
| 15 | 15 | Uses | |
| 16 | 16 | Variants; | |
| 17 | 17 | var | |
| 18 | 18 | v: Variant; | |
| 19 | 19 | ``` | |
| 20 | 20 | ||
| 21 | 21 | Example usage: | |
| 22 | 22 | ||
| 23 | 23 | ``` pascal | |
| 24 | 24 | uses | |
| 25 | 25 | Variants; | |
| 26 | 26 | var | |
| 27 | 27 | v: Variant; | |
| 28 | 28 | s: string; | |
| 29 | 29 | i: integer; | |
| 30 | 30 | ||
| 31 | 31 | procedure ProcessVariant(v : Variant); | |
| 32 | 32 | begin | |
| 33 | 33 | ||
| 34 | 34 | // Example of how to determine the contents of a Variant type: | |
| 35 | 35 | Case varType(v) of | |
| 36 | 36 | varEmpty: | |
| 37 | 37 | Writeln('Empty'); | |
| 38 | 38 | varNull: | |
| 39 | 39 | Writeln('Null'); | |
| 40 | 40 | varSingle: | |
| 41 | 41 | Writeln('Datatype: Single'); | |
| 42 | 42 | varDouble: | |
| 43 | 43 | Writeln('Datatype: Double'); | |
| 44 | 44 | varDecimal: | |
| 45 | 45 | Writeln('Datatype: Decimal'); | |
| 46 | 46 | varCurrency: | |
| 47 | 47 | Writeln('Datatype: Currency'); | |
| 48 | 48 | varDate: | |
| 49 | 49 | Writeln('Datatype: Date'); | |
| 50 | 50 | varOleStr: | |
| 51 | 51 | Writeln('Datatype: UnicodeString'); | |
| 52 | 52 | varStrArg: | |
| 53 | 53 | Writeln('Datatype: COM-compatible string'); | |
| 54 | 54 | varString: | |
| 55 | 55 | Writeln('Datatype: Pointer to a dynamic string'); | |
| 56 | 56 | varDispatch: | |
| 57 | 57 | Writeln('Datatype: Pointer to an Automation object'); | |
| 58 | 58 | varBoolean: | |
| 59 | 59 | Writeln('Datatype: Wordbool'); | |
| 60 | 60 | varVariant: | |
| 61 | 61 | Writeln('Datatype: Variant'); | |
| 62 | 62 | varUnknown: | |
| 63 | 63 | Writeln('Datatype: unknown'); | |
| 64 | 64 | varShortInt: | |
| 65 | 65 | Writeln('Datatype: ShortInt'); | |
| 66 | 66 | varSmallint: | |
| 67 | 67 | Writeln('Datatype: Smallint'); | |
| 68 | 68 | varInteger: | |
| 69 | 69 | Writeln('Datatype: Integer'); | |
| 70 | 70 | varInt64: | |
| 71 | 71 | Writeln('Datatype: Int64'); | |
| 72 | 72 | varByte: | |
| 73 | 73 | Writeln('Datatype: Byte'); | |
| 74 | 74 | varWord: | |
| 75 | 75 | Writeln('Datatype: Word'); | |
| 76 | 76 | varLongWord: | |
| 77 | 77 | Writeln('Datatype: LongWord'); | |
| 78 | 78 | varQWord: | |
| 79 | 79 | Writeln('Datatype: QWord'); | |
| 80 | 80 | varError: | |
| 81 | 81 | Writeln('ERROR determining variant type'); | |
| 82 | 82 | else | |
| 83 | 83 | Writeln('Unable to determine variant type'); | |
| 84 | 84 | end; | |
| 85 | 85 | end; | |
| 86 | 86 | ||
| 87 | 87 | begin | |
| 88 | 88 | ||
| 89 | 89 | // Example 1: | |
| 90 | 90 | // Value assignment: | |
| 91 | 91 | v := 1; | |
| 92 | 92 | ||
| 93 | 93 | //Examples of permissible type assignments, auditions / conversions of data type: | |
| 94 | 94 | ||
| 95 | 95 | //Explicit data type conversions: | |
| 96 | 96 | s := VarToStr(v); | |
| 97 | 97 | Writeln('s:',s); | |
| 98 | 98 | s := AnsiString(v); | |
| 99 | 99 | Writeln('s:',s); | |
| 100 | 100 | i := Integer(v); | |
| 101 | 101 | Writeln('i:',i); | |
| 102 | 102 | //Implicit data type conversion: | |
| 103 | 103 | i := v; | |
| 104 | 104 | Writeln('i:',i); | |
| 105 | 105 | ||
| 106 | 106 | ProcessVariant(10); | |
| 107 | 107 | ProcessVariant(True); | |
| 108 | 108 | ProcessVariant('Pascal'); | |
| 109 | 109 | ProcessVariant(10.244); | |
| 110 | 110 | ||
| 111 | 111 | end. | |
| 112 | 112 | ``` | |
| 113 | 113 | ||
| 114 | 114 | Outputs: | |
| 115 | 115 | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 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 | + | ``` | |
| 124 | 126 | ||
| 125 | 127 | --- | |
| 126 | 128 | *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).* |
