Solved

# dyn. array as parameter of a procedure

Posted on 2001-09-18
349 Views
I need a procedure to fill a dyn. array. How to do that? The following example cause an error at "SetLength(v, 10);". Can you correct the example?

procedure FillArray(var v:array of integer);
begin
SetLength(v, 10);
v[5]:=12;
end;

procedure TForm1.Button1Click(Sender: TObject);
var v:array of integer;
begin
FillArray(v);
if v[5]=12 then beep;
end;
0
Question by:bengore

LVL 20

Accepted Solution

ID: 6489779
When you declare a function like...

procedure FillArray(var v: array of xxx);

... you can't change the length of the array, because you can also call "FillArray" with a static array parameter. How shall FillArray know whether the parameter is a static or a dynamic one?

You have to declare a dynamic array type like this:

type TDAInteger = array of integer;

procedure FillArray(var v: TDAInteger);
begin
SetLength(v, 10);
v[5] := 12;
end;

procedure TForm1.Button1Click(Sender: TObject);
var v : TDAInteger;
begin
FillArray(v);
if v[5] = 12 then beep;
end;

0

LVL 7

Expert Comment

ID: 6489786
You must allocate space befor pass it to your fill procedure:

procedure FillArray(var v:array of integer);
begin
v[5]:=12;
end;

procedure TForm1.Button1Click(Sender: TObject);
var v:array of integer;
begin
SetLength(V, 10);
FillArray(v);
if v[5]=12 then beep;
end;

Motaz
0

Author Comment

ID: 6489801
0

## Featured Post

### Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.