What you have posted there won't compile.
 
For starters you can't return an Array of Integer from a function you must make a new type that is array of integer and return that.
 
ie.
type
  ArrayOfInt = array of Integer;
 
function GetMyInts: ArrayOfInt;
 
Now as you have a dynamic array, you need to set the size.
Therefore you need
 
SetLength(result, 11);
 
then your for loop would be
 
for i := 0 to 10 do
  result[i] := i;
 
then in a button event you could have something like
 
var
  lIntArray: ArrayOnInt;
  i: Integer;
begin
  lIntArray := GetMyInts;
  for i := 0 to High(lIntArray) do
    Listbox1.Items.Add(IntToStr(lIntArray[i])); // put items in a listbox
end;
 
Alternatively you could pass your array variable as a VAR parameter.
Below is some code to demostrate a function and procedure that can be updated.
(Need a Button and ListBox on a form)
 
type
  ArrayOnInt = array of Integer;
 
function GetMyInts: ArrayOnInt; overload;
var
  I: Integer;
begin
  SetLength(result, 11);
  for i := 0 to 10 do
    result[i] := i;
end;
 
procedure GetMyInts(var aArray: ArrayOnInt); overload;
var
  I: Integer;
begin
  SetLength(aArray, 11);
  for i := 0 to 10 do
    aArray[i] := i;
end;
 
procedure TForm3.Button1Click(Sender: TObject);
var
  lIntArray: ArrayOnInt;
  i: Integer;
begin
  //lIntArray := GetMyInts;
  GetMyInts(lIntArray);
  for i := 0 to High(lIntArray) do
    Listbox1.Items.Add(IntToStr(lIntArray[i]));
end;
HTH,
 
JED
 
-----Original Message-----
From: Paul McKenzie [mailto:[EMAIL PROTECTED]
Sent: 7 December 2004 1:20 PM
To: Delphi List - Delphi
Subject: [DUG] Returning Open Arrays

How do I Build and Return an Open Array
I know I can do this with a TList or other means - but seems over the top...
 
eg
function GetMyInts: Array of Integer;
var
    I: Integer;
begin
    for I := 0 to 10 do
        Result := Result + I;
end;
 
 
Regards
Paul McKenzie
SMSS Ltd.
Wellington
New Zealand
_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to