Results 1 to 6 of 6

Thread: Array of Objects

  1. #1

    Array of Objects

    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.

  2. #2

    Array of Objects

    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.
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  3. #3
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Array of Objects

    Why not make a List of TList objects?
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  4. #4

    Array of Objects

    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

    [pascal]
    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;
    [/pascal]

    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).

  5. #5

    Array of Objects

    TheLion posted an alternative solution but it vanished in the server move. Here it is again: multidimensional dynamic arrays.

    [pascal]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;[/pascal]

    Syntax may be a little off because I've not used multidimensional dynamic arrays much.
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  6. #6

    Array of Objects

    Thanks for reposting it AliMonster!
    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!
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •