Passing a dynamic array in a parameter

This is what i have been trying:
 
type
   MyArray = array of integer;

function Test ( a : MyArray):boolean;
begin

end;

procedure DoTheTest;
   var
     b : array of integer;

begin
    Test(b);
end;

Open in new window


but i get the error E2010 Incompatible types: 'MyArray' and 'dynamic array'

What i want to do is pass the dynamic array as a pointer - its huge so i don't want a copy of it.
Paer ToernellAsked:
Who is Participating?
 
cyberkiwiCommented:
Just use the same type

procedure DoTheTest;
   var
     b : MyArray;

begin
    Test(b);
end;
0
 
jimyXCommented:
Or the other way:
function Test ( a : array of integer):boolean;
begin

end;

procedure DoTheTest;
   var
     b : array of integer;

begin
    Test(b);
end;

Open in new window

0
 
jimyXCommented:
Also you can cast it:
type
   MyArray = array of integer;

function Test ( a : MyArray):boolean;
begin

end;

procedure DoTheTest;
var
  b : array of integer;
begin
  if Test(MyArray(b)) then
      //ShowMessage('Works');
end;

Open in new window

0
 
systanCommented:
I don't know how will you use the functions, so I just add some important lines before declaring the array.

function Test ( a : MyArray):boolean;
begin
SetLength(a, length_of_your_dynamic_array);  //first line <must>
...
end;
0
 
Ephraim WangoyaCommented:

You can use a pointer or a var parameter, either of these will send the address of the array
type
  PMyArray = ^TMyArray;
  TMyArray = array of Integer;

//Method 1
function Test(A: PMyArray):boolean;
begin

end; 

procedure DoTheTest;
var
  b : TMyArray;
begin
  Test(@b);
end;


//Method 2
function Test(var A: TMyArray):boolean;
begin

end;

procedure DoTheTest;
var
  b : TMyArray;
begin
  Test(b);
end;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.