VarToWideStrDef converts a variant type variable or field V, into a widestring.

But unlike the similar function VarToWideStr, if the variant is null (has a vartype of 1), then the specified default string value ADefault will be returned.

Whereas a widestring variable containing text returns a vartype of 8, a variant that contains text returns a vartype of 256, so the two variable types are considered to be different and unique.

Unicode characters are handled with ease in both widestring and variant data types.

Please also review TVarType for more information.

This function is more commonly used with variant type variables, rather than with fields. 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 VarToWideStr for an alternative function.

Declaration: Function VarToWideStrDef(const V : Variant; const ADefault : WideString) : WideString;

An example.

procedure OnMapEvent(var Value:Variant);
begin
Value := VarToWideStrDef(Field.Value, 'abc');
end;

The function can also be responsible for changing the VarType as illustrated in the sample code that follows - from 256 to 8.

procedure ScriptEvent (var Value : variant);
var
aVar, dVar, fVar : variant;
aWVar, cWVar, aWMess, DefVar, fWVar : widestring;
begin
LogInfo('');
//aVar = variant type
aVar := 'Hello there βÆ£€èç';
LogInfo('Text in a VARIANT variable (V) ........ '+ aVar);
//Vartype is 256
LogInfo('Data Type for VARIANT variable with TEXT = '+IntToStr(VarType(aVar)));
LogInfo('');
 
//aWVar = widestring type
aWVar := 'What is up? βÆ£€èç';
LogInfo('Text in a WIDESTRING variable (V) ........ '+ aWVar);
//VarType is 8
LogInfo('Data Type for WIDESTRING variable with TEXT = '+IntToStr(VarType(aWVar)));
LogInfo('');
 
aWMess := 'This is replacement TEXT as a default message.';
 
//cWVar = widestring type
cWVar := VarToWideStrDef(aVar, aWMess);
//Returns 'Hello there βÆ£€èç'
LogInfo('Result is ................ ' + cWVar);
//VarType is 8
LogInfo('Data Type for converted VAR->WIDESTRING variable with TEXT into WIDESTRING variable = '+IntToStr(VarType(cWVar)));
LogInfo('');
 
//dVar = variant data type
dVar := VarToWideStrDef(aVar, 'Default VALUE');
//Returns 'Hello there βÆ£€èç'
LogInfo('Result is ................ ' + dVar);
//VarType is 8
LogInfo('Data Type for converted VAR->WIDESTRING variable with TEXT into VARIANT variable = '+IntToStr(VarType(dVar)));
LogInfo('');
 
//fVar = variant type
fVar := null;
fWVar := VarToWideStrDef(fVar, aWMess);
//Returns 'This is replacement TEXT as a default message.'
LogInfo('Result is ................ *' + fWVar + '*');
//VarType is 8
LogInfo('Data Type for Null VARIANT variable converted into WIDESTRING = '+IntToStr(VarType(fWVar)));
LogInfo('');
 
//DefVar = widestring type
DefVar := VarToWideStrDef(fVar, 'Hello possums!');
//Returns 'Hello possums!'
LogInfo('Result is ................ ' + DefVar);
//VarType is 8
LogInfo('Data Type for Null VARIANT variable converted into VARIANT variable = '+IntToStr(VarType(DefVar)));
LogInfo('');
 
end;