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
Delphi

Avatar of undefined
Last Comment
edey
ASKER CERTIFIED SOLUTION
Avatar of edey
edey

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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.
Avatar of edey
edey

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

GL
Mike
Avatar of retep
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

Avatar of rwilson032697
rwilson032697

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.



 
Avatar of lordcrc
lordcrc

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
Avatar of rwilson032697
rwilson032697

As per my suggestion :-)
Avatar of retep
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
Avatar of edey
edey

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

GL
Mike
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo