Solved

dyn. array as parameter of a procedure

Posted on 2001-09-18
3
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

690 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