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
retepAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

edeyCommented:
you can declare a pointer to TObject like this


TObjectArray = array[0..0] of TObject
PObjectArray = ^TObjectArray;

then declare A:

a : PObjectArray;

and get/release mem for it:


getMem(a,sizeOf(TObject)*max);
dispose(a);


GL
Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwilson032697Commented:
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.
0
edeyCommented:
should note that, while both of our solutions use 'dynamic' arrays, rwilson's requires d4+.

GL
Mike
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

retepAuthor Commented:
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

0
rwilson032697Commented:
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.



 
0
lordcrcCommented:
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
0
rwilson032697Commented:
As per my suggestion :-)
0
retepAuthor Commented:
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
0
edeyCommented:
thank you :)
hope we have been helpfull, dynamic rasource management is an important topic, one def. worth exploring more :)

GL
Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.