Variant
Variant is a special data type that is capable of holding any type of data.
For instance, a variable defined as a Variant could be set to a numeric value, a boolean, or a String value.
.Value is used to get or set the value of the field as a Variant. When referencing a field using this .Value property, you will have the field returned as a Variant, which means along with any actual data, it could also contain the value of empty or null.
But be mindful that a null value is different from an empty value.
As well as using .Value, you can reference the field using either .AsString, .AsInteger, or .AsFloat to have any null values converted into a blank String, zero (0), or 0.0000 respectively.
Any database field that contains a null value will be returned as null.
Check out the section on Testing For Null (empty or blank) Values for additional details.
For example, using MyVariant as the variable name, the variable is set to be a blank String using MyVariant := ''; , while the variable is set to null with MyVariant := null; as below.
// To set a variable as a blank string
MyVariant :=
''
;
// To set a variable as a null string
MyVariant := null;
You will receive errors if passing a null variant to a function that expects parameters to be a specific data type, such as Integer or String etc. Please also refer to Scripting Reference for more information.
Once you give a Variant a value and therefore a data type, an internal flag is set which indicates what data type the Variant represents. From that point onwards, you should not try and change the data type, but should treat the Variant as only that particular data type which was originally set.
There is a potential danger when concatenating strings and numbers without explicit conversion - refer to String (String, Widestring) for more detail.
For instance if you set a Variant value to 123.45, it is flagged as a floating-point number and therefore you should explicitly convert it when concatenating to a String.
Declaration: var myvariable : variant
This example shows four different data types being set.
procedure
ScriptEvent (
var
Value : variant);
var
myvariable : variant;
begin
myvariable :=
123.45
;
//A floating-point value
myvariable :=
'This is some text'
;
//A string
myvariable := Date;
//Setting it as a date variable
myvariable := null;
//Setting it with a null value
myvariable :=
''
;
//An empty string
myvariable :=
' '
;
//A blank string
end
;
You can work with Variants using the available Variant functions. Variant Functions will provide more guidance.
For a list of the available variant data types, please review TVarType.