Solved

dyn. array as parameter of a procedure

Posted on 2001-09-18
3
353 Views
Last Modified: 2010-04-06
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
Comment
Question by:bengore
3 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
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;

Regards, Madshi.
0
 
LVL 7

Expert Comment

by:Motaz
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

by:bengore
ID: 6489801
Thank you Madshi!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question