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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create & call an indeterminate number of instances?

Status
Not open for further replies.

Han777

Programmer
Dec 12, 2007
19
I need to create multiple instances of an object, then keep calling any or all of those instances until no longer needed, at which time any can selectively be destroyed.

I would like to know a programmatic way of creating and calling each instance, naming each incrementally, without knowing ahead of time how many instances may need to be created. For example, when the code detects a condition that requires an instance be created the code would assign an instance to a variable of the same type, then call it by referencing the variable's name.

How can I write code that creates those instances without having to manually create an unknown number variables in the code of the object type I'm needing, as I have no way of knowing how many instances I will be needing to create and call?
 
you can use TObjectlist for this purpose, you can find it in the Contnrs unit.


here's a small example how you could do it:
Code:
type
  MyObject = class(TObject)
  private
    FName : string;  
  public
    property Name : string read FName write FName;  
  end;

//create your objectlist, I am gonna assume you are building a forms app, so the obvious place is formcreate

  TFrm_main = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender : TObject);
  private
    UniqueCounter : Integer;
    MyObjects : TObjectList;
    function CreateMyObject : TMyObject;
    procedure DestroyMyObject(MyObject : TMyObject);
    function GetMyObjectByName(Name : string) : TMyObject;
  ...
  end;

...
implementation

function TFrm_main.CreateMyObject : TMyObject;
begin
 Result := TMyObject.Create;
 // create unique name
 Inc(UniqueCounter); 
 Result.Name := Format('MyObject_%d', [UniqueCounter]);
 // keep reference of object in our list for later retrieval
 MyObjects.Add(Result);
end;

procedure TFrm_main.DestroyMyObject(MyObject : TMyObject);

var Index : Integer;

begin
 Index := MyObjects.IndexOf(MyObject);
 if Index >  -1 then
  MyObjects.Delete(MyObject); // this will also free the object
end;

function TFrm_main.GetMyObjectByName(Name : string) : TMyObject;

var Index : Integer;

begin
 // lookup object by name 
 Result := nil;
 Index := MyObjects.Count;
 while Index > 0 do
  begin
   Dec(Index);
   if TMyObject(MyObjects[Index]).Name = Name then
    begin
     Result := TMyObject(MyObjects[Index]);
     Break;
    end;
  end;
end;

procedure TFrm_main.Button1Click(Sender : TObject);

var MyObject : TMyObject;

begin
  MyObject := CreateMyObject;
  ShowMessage(MyObject.Name);
end;

procedure TFrm_main.Button2Click(Sender : TObject);

var MyObject : TMyObject;

begin
  // retrieve our created object
  MyObject := GetMyObjectByName('MyObject_1');
  if Assigned(MyObject) then
   begin
    // do something with myObject
    MyObject.DoSomething;
    ...
    DestroyMyObject(MyObject); 
   end;
end;

procedure TFrm_main.FormCreate(Sender: TObject);
begin
 // the True value here indicates that the object will be freed upon deletion 
 MyObjects := TObjectList.Create(True);
 UniqueCounter := 0;
end;

procedure TFrm_main.FormDestroy(Sender: TObject);
begin
 if Assigned(NodeObjects) then
  FreeAndNil(NodeObjects);
end;

...

I hope this clarifies the works.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
That's a generous bit of code Daddy. Thanks! I don't have ANY experience with forms as I create dlls only. The Forms referencing confuses me a bit. I'll have to work a bit to sort through this but its a start which I much appreciate. If the rich bounds of your generosity and time have not yet been crossed, what is the possibility you could revise the example as a unit in a dll? If you're too busy answering other questions, no problem. I appreciate thus much direction.

Thanks again!
 
my code is still valid for a dll

only change is the init part since you don't have a form:

Code:
initialization
 MyObjects := TObjectList.Create(True);
 UniqueCounter := 0;
finalization
 if Assigned(NodeObjects) then
  FreeAndNil(NodeObjects);
end.

put this code block at the end of your unit (replacing the "end." part

/daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
That's a generous bit of code Daddy. Thanks!
Then why not reward daddy with a star? Click on the [red]*[/red][blue]Thank whosrdaddy for this valuable post![/blue] at the bottom left hand corner of his posting.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top