Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array of objects 2

Status
Not open for further replies.

ahhchu

Programmer
Sep 19, 2001
38
US
I am trying to create and use an array of objects. I get a memory reference when instantiating the objects of the array


MyObjectA : array of MyObject;

MyObjectA[0] := MyObject.Create;

results in a memory ref error. Is there a different way to do this...

thanks
 
Whether or not it's an ideal way of achieving this I'll suggest what I've done in the past. I've created a TList and then added objects to this list. i.e.
MyList.Add(Query1);
MyList.Add(Query2);
etc.
In order to use the objects in this list as their appropriate type (i.e. TQuery) I use something like 'TQuery(MyList[x]).Active := True;'.
Hope that this helps.
Steve
 
Howdy,

I'm assuming you haven't set the size of your object array. Since you have declared it as a dynamic array, before you can use it, you need to set the size of the array. For example :

var
MyObjectA : array of MyObject;
begin
SetLength(MyObjectA, 9);
MyObjectA[0] := MyObject.Create;
end;

Hope this helps.
WB

 
Thanks all,
I have a variable # of objects and would like to use a TLIST but I am missing how to Create an instance of an object dynamically. I want to use

New(MyObject)

and assign it to a reference and then to the TLIST. I can declare the instances of the object like:

MyObject1,MyObject2 : TMyObject;

and Create them but I am limited to the 2. Is there a way around this? There must be but I am not getting it at the moment.

Thanks
ahhchu

 
for x := 1 to 10 do
begin
MyList.Add(MyObject.Create());
end

Does that make sense? MyObject.Create() returns a MyObject which you are adding to the list.

Hope that helps!
TealWren
 
TealWren,
Thanks! that does make sense ! and helps a lot....

Thanks Again!
ahhchu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top