VarToStrDef
In a similar fashion to the function VarToStr, the function VarToStrDef converts a variant type variable or field V, into a string.
If the variant is null, then the specified default string value ADefault will be returned.
Unicode characters are converted without issue.
If you do not require a default value to be included when accessing fields, you can achieve the same conversion result by using the Field Class FieldAsString property. However it is necessary to use this function with fields if you do want a default value.
Please also review VarToStr.
Declaration: Function VarToStrDef(const V : Variant; const ADefault : string) : string;
An example.
procedure
OnMapEvent(
var
Value:Variant);
begin
Value := VarToStrDef(Field
.
Value,
'abc'
);
end
;
As illustrated below, whereas a string variable containing text returns a vartype of 258, a variant that contains text returns a vartype of 256, so the two variable types are considered to be different and unique. The function can also be responsible for changing the VarType as illustrated in the sample code that follows - from 256 to 258. Please also review TVarType for more information.
procedure
ScriptEvent (
var
Value : variant);
var
aVar, dVar, DefVar : variant;
bVar, cVar, DefStr :
string
;
begin
LogInfo(
''
);
LogInfo(
'======== V parameter holds TEXT ======='
);
aVar :=
'Hello there'
;
LogInfo(
'Text in a VARIANT variable (V) ........ '
+ aVar);
//VARIANT variable containing text returns VarType of 256
LogInfo(
'VARIANT variable with TEXT aVar = '
+IntToStr(VarType(aVar)));
LogInfo(
''
);
bVar :=
'What is up?'
;
LogInfo(
'Comparing text in a STRING variable ........ '
+ bVar);
//STRING variable containing text returns VarType of 258
LogInfo(
'STRING variable with TEXT bVar = '
+IntToStr(VarType(bVar)));
LogInfo(
''
);
cVar := VarToStrDef(aVar,
'This is the replacement text.'
);
LogInfo(
'Result is ........ '
+ cVar);
//VARIANT converted into STRING variable returns VarType of 258
LogInfo(
'Converted VAR->STR variable with TEXT into STR variable = '
+IntToStr(VarType(cVar)));
LogInfo(
''
);
dVar := VarToStrDef(aVar,
'This is the replacement text.'
);
LogInfo(
'Result is ........ '
+ dVar);
//VARIANT converted into VARIANT variable returns VarType of 258
LogInfo(
'Converted VAR->STR variable with TEXT into VAR variable = '
+IntToStr(VarType(dVar)));
LogInfo(
''
);
LogInfo(
'======== V parameter forced to NULL ======='
);
aVar := null;
DefStr := VarToStrDef(aVar,
'This is the replacement text.'
);
LogInfo(
'Result is ......... '
+ DefStr);
//VARIANT converted into STRING variable returns VarType of 258
LogInfo(
'Converted VAR->STR variable with TEXT into STR variable = '
+IntToStr(VarType(DefStr)));
LogInfo(
''
);
DefVar := VarToStrDef(aVar,
'This is the replacement text.'
);
LogInfo(
'Result is .......... '
+ DefVar);
//VARIANT converted into VARIANT variable returns VarType of 258
LogInfo(
'Converted VAR->STR variable with TEXT into VAR variable = '
+IntToStr(VarType(DefVar)));
LogInfo(
''
);
end
;