Solved

Dynamic  instantiation of classes

Posted on 2000-04-08
8
163 Views
Last Modified: 2013-11-23
I have a certain class defined. Let's say:
class someClass
   constructor create(const a: integer);
   ...
end;

During runtime I need to create instances of that class, yet I cannot know until rutime how many objects will be required (of course, for each instance of the class, the constructor should be called).

What would be the best way to create instances of this class in runtime and manage a list of all the object I have created from that class so I can access their methods etc.?

Note that instances of this class shoukd be created from within a function yet remain viable after the function exits because other functions should use those objects.
0
Comment
Question by:mathought
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 2696150
uses cntnrs;

var CList : TObjectList;


procedure SomeCreate;
var I : integer;
begin
   for I:=1 to 50 do CList.Add(TYourClass.Create(I));
end;


procedure DoSomething;
begin
   TYourClass(CList[5]).YourMethod;
end;
----
Igor.

Do not forget to initialize CList;


0
 

Author Comment

by:mathought
ID: 2696637
I use Delphi 4 and:
Does not recognize "TObjectList" nor "uses cntnrs"

anyway, I used TList instead of TObjectList and it worked. (Am I missing anything?)
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2696698
That should be contnrs


Or use TList instead:

CList.Add(Pointer(TYourClass.Create));


The rest is like Igor says.

Regards,

Epsylon.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:mathought
ID: 2696720
Helped me, but was a little bit inaccurate.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2696740
>but was a little bit inaccurate

How about 'Thanks' or something like that? We answer questions in our own time you know...
0
 

Author Comment

by:mathought
ID: 2696745
You are right, sorry.
I truely thank you both.

But The points did belong to Igor.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2696757
>The points did belong to Igor

No problem, I agree  :o)

Cheers,

Epsylon.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2697535
The main difference of TList and TObjectList:

----- TObjectList Delphi help --------
If the OwnsObjects property is set to True (the default), TObjectList controls the memory of its objects, freeing an object when its index is reassigned; when it is removed from the list with the Delete, Remove, or Clear method; or when the TObjectList instance is itself destroyed.
----- end------

TList did't free items automatically.

-----
Igor.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 143
QRReport  TQrmemo vertical stretching 1 46
select query - oracle 16 100
SUM 2 INTEGER ARRAYS INTO 1 10 100
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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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