[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

A question about arrays in classes...

Posted on 2000-02-10
9
Medium Priority
?
167 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:retep
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Accepted Solution

by:
edey earned 200 total points
ID: 2509052
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
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2509218
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
 
LVL 6

Expert Comment

by:edey
ID: 2509337
should note that, while both of our solutions use 'dynamic' arrays, rwilson's requires d4+.

GL
Mike
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 

Author Comment

by:retep
ID: 2509497
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
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2509523
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
 

Expert Comment

by:lordcrc
ID: 2509665
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
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2509673
As per my suggestion :-)
0
 

Author Comment

by:retep
ID: 2509836
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
 
LVL 6

Expert Comment

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

GL
Mike
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question