Skip to content

VarToStr ​

VarToStr converts a variant type variable or field V, to a string.

If the variant is null (has a vartype of 1), then it will return a blank string.

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.

Unicode characters are handled with ease.

Please also review TVarType for more information.

This function is more commonly used with variant type variables, rather than fields. However, when accessing fields, you can achieve the same thing by using the Field Class object FieldAsString property.

Please also review VarToStrDef for an alternative function.

Declaration: Function VarToStr(const V : Variant) : string

An example.

pascal
procedure OnMapEvent(var Value:Variant);
begin
  Value := VarToStr(Field.Value);
end;

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

pascal
procedure ScriptEvent (var Value : variant);
var
  aVar, dVar : variant;
  bVar, cVar : string;
begin
  LogInfo('');

 //VARIANT variable containing text returns VarType of 256
  aVar := 'Hello there';
  LogInfo('VARIANT variable with TEXT aVar = '+IntToStr(VarType(aVar)));
  LogInfo('');

 //STRING variable containing text returns VarType of 258
  bVar := 'What is up?';
  LogInfo('STRING variable with TEXT bVar = '+IntToStr(VarType(bVar)));
  LogInfo('');

 //STRING variable used to convert into returns VarType of 258
  cVar := VarToStr(aVar);
  LogInfo('Converted VAR->STR variable with TEXT into STR variable cVar = '+IntToStr(VarType(cVar)));
  LogInfo('');

 //VARIANT variable used to convert into returns VarType of 258
 //So the variable VarType is actually altered from 256 to 258
  dVar := VarToStr(aVar);
  LogInfo('Converted VAR->STR variable with TEXT into VAR variable dVar = '+IntToStr(VarType(dVar)));
  LogInfo('');

 end;