Since variant under Quartex Pascal maps to the default JavaScript data-type, this also means that it's the most tricky to work with.

A variant can contain a number, a string, an object, a native object reference, a dictionary, a reference to a procedure, an associate array, a typed array; in short: a variant has the potential to contain or reference the full scope of everything JavaScript has to offer.


To simplify working with variants, a utility class was created called TVariant. This class can be found in qtx.sysutils.pas. In the same unit there are also other, similar classes for string and integer. Cleverly named TString and TInt32, both of which contains functions that simplify common tasks.


Note: These classes are all abstract and expose class methods. You do not create instances of these classes, but call the methods directly.


The TVariant class has the following interface:


TVariant = static class abstract

public

 class function AsInteger(aValue: variant): int32; static;

 class function AsString(aValue: variant): string; static;

 class function AsFloat(aValue: variant): float; static;

 class function AsObject(aValue: variant): TObject; static;

 class function AsBool(aValue: variant): boolean; static;


 class function IsArray(aValue: variant): boolean; static;

 class function IsNull(aValue: variant): boolean; static;

 class function IsUnDefined(aValue: variant): boolean; static;

 class function IsString(aValue: variant): boolean; static;

 class function IsNumber(aValue: variant): boolean; static;

 class function Isint32(aValue: variant): boolean; static;

 class function IsBool(aValue: variant): boolean; static;

 class function IsNAN(aValue: variant): boolean; static;

 class function ClassInstance(instance: variant): boolean; static;

 class function CreateObject: variant; static;

 class function CreateArray: variant; static;


 class function ExamineType(Value: variant): TVariantExportType; static;


 class function GetKeys(&Object: variant): TStrArray; static;

 class function Properties(Data: variant): TStrArray; static;

 class function ProperyCount(Data: variant): int32; static;

 class function PropertyExists(Data: variant; Field: string): boolean; static;

 class function PropertyRead(Data: variant; Field: string): variant; static;

 class function PropertyWrite(Data: variant; Field: string; Value: variant): variant; static;

 class function PropertyDelete(Data: variant; Field: string): variant; static;


 class function FromObject(&Object: TObject): variant; static;

 class function ToObject(Value: variant): TObject; static;


 class procedure Assign(&Object: variant; var target: variant); static;

 class function Clone(&Object: variant): variant; static;


 class procedure ForEachProperty(&Object: variant; CB: TJSObjectKeyCB); static;


 class function SearchAssociate(Associate: variant; Field: string; Value: variant): variant; static;

end;


To give an example of use, let's say you want to check if a variant contains an array object:


if TVariant.IsArray( Value ) then

begin

 // iterate through each item in the array

 for var item in value do

 begin

 end;

end;