Link to home
Create AccountLog in
Avatar of Paer Toernell
Paer ToernellFlag for Thailand

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of jimyX
jimyX

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

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

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;

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