PDA

View Full Version : Array of Objects



Viro
04-02-2003, 06:02 PM
In Delphi, is an array of Objects treated as an object, like in Java? The reason I'm asking this is to see if I'm able to put and array of Objects into a TList (Or something similar).

EDIT: just to be more clear, what I'm interested in doing is creating a dynamic jagged array of objects. If there's a better way to do that in Delphi, do let me know.

Thanks.

TheLion
04-02-2003, 06:45 PM
I think you can (theoretically) if you make a record of a dynamic array :

Type
PMydyn : ^MyDyn;
MyDyn : Array of TObject;

In that case you should be able to put the PMyDyn into the TList... I never tried anything like this, however the TList is nothing more than a list of pointers.

cairnswm
04-02-2003, 07:17 PM
Why not make a List of TList objects?

Viro
04-02-2003, 11:23 PM
Thankfully, TList takes pointers, and just like good old C/C++, and array is basically a pointer in Delphi :) Whew.

What I have now is something like


type
TIntArray = array of integer;

var
MyArray : array of TIntArray;
i : integer;
begin
//array that holds 10 arrays
SetLength(MyArray, 10);

for i := 0 to Length(MyArray) do
begin
//random number of elements in the array
SetLength(MyArray, random(100));
end;
end;


It works for native types, and I don't think it would be a problem with TObjects either. But being rather unfamiliar with Pascal, if anyone sees a problem with the code above, do tell me (yes, I know it doesn't do anything useful, its just an example).

Alimonster
05-02-2003, 07:07 PM
TheLion posted an alternative solution but it vanished in the server move. Here it is again: multidimensional dynamic arrays.

var
MyArray: array of array of Integer;
i: Integer;
begin
SetLength(MyArray, 5);
for i := 0 to 4 do
SetLength(MyArray[i], i+1);

// total count of arrays
ShowMessage(IntToStr(Length(MyArray)));

// get the length of each array
for i := 0 to 4 do
ShowMessage(IntToStr(Length(MyArray[i])));
end;

Syntax may be a little off because I've not used multidimensional dynamic arrays much.

TheLion
05-02-2003, 09:48 PM
Thanks for reposting it AliMonster! :D
As far as I could determine it your syntax is correct. The example I posted was simply:

var ObjectLst : Array of Array of TObject;

However AliMonsters explanation is much more helpful! ;)