Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

passing array as a parameter

Posted on 2004-04-12
1
Medium Priority
?
806 Views
Last Modified: 2010-04-05
hi, everyone.

my code is this:

procedure TForm1.Beregn(var Vinner: array of TKort);
var
j: integer;
begin
SetLength(Vinner,j);
dosomething with Vinner;
SetLength(Vinner,j);
end;

I call this metdhod this way:

procedure TForm1.Test;
var
Vinner: array of TKort;
begin
Beregn(Vinner);
Continue with my Vinner array that has been changed inside Beregn-method.
end;


But the thing is... I get an error everytime i try to call the SetLength method inside the Beregn method. Incompatible types, it says. Then I thought... is this a pointer-issue?
0
Comment
Question by:Darth_helge
1 Comment
 
LVL 20

Accepted Solution

by:
Madshi earned 2000 total points
ID: 10804708
When using an open "array of" array parameter like that you can't change the length of the array. The reason is simple:

procedure TForm1.Test;
var Vinner: array [0..9] of TKort;
begin
  Beregn(Vinner);
end;

What should Delphi do now, if you call SetLength? Do you see the problem? That's why calling SetLength is not supported.

However, you can solve the problem this way:

type TVinnerArr = array of TKort;

procedure TForm1.Beregn(var Vinner: TVinnerArr);
var j: integer;
begin
  SetLength(Vinner,j);
  dosomething with Vinner;
  SetLength(Vinner,j);
end;

procedure TForm1.Test;
var Vinner: TVinnerArr;
begin
  Beregn(Vinner);
end;

This works.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

877 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