procedure DoSomething(var res: array of byte);
var
i: integer;
begin
for i := Low(res) to High(res) do something()
end;
And from another procedure I call DoSomething like this:
procedure Button1Click(Sender: TObject);
var
myArray: array of byte;
begin
SetLength(myArray, GetSomeNumber());
DoSomething(myArray) ***
end;
I get an error at *** saying that an array and a dynamic array are not
compatible and thus I cannot do what I intended to do. But I have to
use dynamic arrays because I only find out about its size a line
before I call DoSomething().
I tried replacing the "array of byte" part in DoSomething() with a
Pointer. This way I don't call SetLength before calling the procedure,
but instead I call it inside DoSomething, like this:
procedure DoSomething(myArray: Pointer);
var
res: array of byte;
i: integer;
begin
SetLength(res, GetSomeNumber());
for i := 0 to GetSomeNumber()-1 do
something;
myArray := res
end;
This didn't work either for some reason. The program did pass
compilation, but I had some exception occuring during runtime.
So what should I do?
Gajo
Res is not a dynamic array. Its an open array parameter. The syntax is
similar, but the implementation is different - see the help. If you want to
use dynamic arrays you need to declare a dynamic array type.
Type tMyByteArray = array of byte;
procedure DoSomething (var res : tMyByteArray);
> procedure Button1Click(Sender: TObject);
> var
> myArray: array of byte;
myArray : tMyByteArray;
Are you sure this will work? I tried this once (a while ago) and it
didn't work for me then.
Oh well, there's only one way to find out...
Thanks for the help ;)
> > Type tMyByteArray = array of byte;
> >
> > procedure DoSomething (var res : tMyByteArray);
>
> Are you sure this will work? I tried this once (a while ago) and it
> didn't work for me then.
Yes I'm sure. If you have a problem then its something else that you are
doing wrong. Should that happen, post the actual code.