Jose Eduardo
Jose Eduardo

Reputation: 487

Getting Data from a VarArray using Delphi

I have a WebService that returns a Variant me that it has an Array ie varArray, would like to know how to get the data that varArray.

Thanks for the help.

Upvotes: 1

Views: 2699

Answers (2)

David Heffernan
David Heffernan

Reputation: 612794

Assuming that the underlying data type is Byte, and that the array is 1-dimensional, the way I would solve this is as follows:

function GetBytesFromVariant(const V: Variant): TBytes;
var
  Len: Integer;
  SafeArray: PVarArray;
begin
  Len := 1+VarArrayHighBound(vArray, 1)-VarArrayLowBound(vArray, 1);
  SetLength(Result, Len);
  SafeArray := VarArrayAsPSafeArray(V);
  Move(SafeArray.Data^, Pointer(Result)^, Length(a)*SizeOf(a[0]));
end;

If the underlying element type is something else, e.g. Word, Integer etc., it should be obvious how to modify this to match.

Upvotes: 0

RRUZ
RRUZ

Reputation: 136391

To get the content of a varArray you must use the VarArrayLowBound and VarArrayHighBound functions, then using a loop you can iterate over the array to get the data.

Try this sample

var
 i : integer;
 s : string; 
begin
  for i := VarArrayLowBound(vArray, 1) to VarArrayHighBound(vArray, 1) do
    s:=vArray[i];//copy the the content of the array i element into a string

Upvotes: 5

Related Questions