Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array and dynamic array

25 views
Skip to first unread message

gajo

unread,
Sep 13, 2004, 2:37:36 PM9/13/04
to
Hi,
I have some troubles getting an array and a dynamic array to work
together. I have a procedure like this:

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

Bruce Roberts

unread,
Sep 13, 2004, 2:56:26 PM9/13/04
to

"gajo" <gcs...@freemail.hu> wrote in message
news:bdc51e61.0409...@posting.google.com...

> Hi,
> I have some troubles getting an array and a dynamic array to work
> together. I have a procedure like this:
>
> procedure DoSomething(var res: array of byte);

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;

gajo

unread,
Sep 14, 2004, 11:18:41 AM9/14/04
to
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message news:<p7m1d.53$9g....@news20.bellglobal.com>...

> 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);

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 ;)

Bruce Roberts

unread,
Sep 14, 2004, 11:52:50 AM9/14/04
to

"gajo" <gcs...@freemail.hu> wrote in message
news:bdc51e61.04091...@posting.google.com...

> > 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.


0 new messages