1

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.

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

I tried to give a ShowMessage in the variable "s" and it returns a blank value
What is the type of the data (string, integer, float) of the array ?
I managed to solve it was an error in the WebService, I sent an email asking to arrumarem ... thank you
VarArrayAsPSafeArray allows direct access to the array and means you can avoid element-wise copying which gets very slow
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.