Link to home
Start Free TrialLog in
Avatar of retep
retep

asked on

A question about arrays in classes...

My question/problem is this:


Interface

  TSomething = Class
  Private
    Max : integer;
    A : Array[1..max] of Tobject;
  Public
    constructor Create(M:integer);
  end;

Implementation

 Constructor TSomething.Create(max:integer);
 Begin
  Max := M;
 end;


The above gives an error since the compiler does not know the 'max'-variable when it tries to make the array.  

This can of course be solved by making 'max' a constant outside the class-definition. But is there not another way to do it, so that the create decides the size of the array???

Retep
ASKER CERTIFIED SOLUTION
Avatar of edey
edey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwilson032697
rwilson032697

You can do it using a dynamic array like this:

Interface

  TSomething = Class
  Private
    Max : integer;
    A : Array of Tobject;
  Public
    constructor Create(M:integer);
  end;

Implementation

 Constructor TSomething.Create(M:integer);
 Begin
   Max := M;
   SetLength(A, M); // Note this is a zero based index so indices are 0..M-1
 end;

Cheers,

Raymond.
should note that, while both of our solutions use 'dynamic' arrays, rwilson's requires d4+.

GL
Mike
Avatar of retep

ASKER

good ideas... As far as I can see, both solutions require Delphi4, since dynamic arrays came with Delphi4. Not a problem because I use Delphi4.

When I use your solutions with dynamic arrays, will the array then be fixed length, or will it still 'grow' dynamically?

Is there not a way to do it with a traditionally fixed-size-array?

Thanks
Retep

The array will not grow dynamically by itself. If you want this to happen you should use a TList or TObjectList.

However, you can resize a dynamic array (ie: one declared like Fred: Array of Tx) any time you like, like this:

Procedure TSomeThing.AddItem(Item : TObject; Index : Integer);
begin
  if Index >= Max then
    begin
      Max := Index + 1);
      SetLength(A, Max);
    end;

  A[Index] := Item;
end;

You can't do it with a trditional fixed length array because, well, they are fixed length. :-)

Cheers,

Raymond.



 
From what you tell, it clearly seems like TList would be the thing to use here. TObjectList is created for this purpose, but i fail to remeber when the delphi version it entered the world (at least its in d5)

The TList would just as easy to use, just to a typecast.

The code would read something like:

type
  TSomething = Class
  private
    FList: TList; // or TObjectList
  public
    constructor Create(Max: cardinal);
    destructor Destroy; override;
  end;

constructor TSomething.Create(Max: cardinal);
begin
  FList:= TList.Create;
  FList.Capacity:= Max;
end;

destructor TSomething.Destroy;
begin
  FList.Free; // of cource free the objects that are in the list first
end;

Now.. to add an object do
FList.Add(MyObject)

to remove you use
Flist.Remove(MyObject)

and to access the objects
MyObject:= TObject(FList.Items[15]);

If you use TObjectList, you dont have to worry about the typecast when fetching objects

- Asbjørn
As per my suggestion :-)
Avatar of retep

ASKER

Thank you all for your comments. It's impossible for me to say which comment is the best since all of them have been very usefull... Therefore I have choosen to accept the first comment added.

Regards
Retep
thank you :)
hope we have been helpfull, dynamic rasource management is an important topic, one def. worth exploring more :)

GL
Mike