How to list objects of a deteminated class?

Asked

Viewed 2,617 times

5

For example, let’s say I have a class TConfiguracao.

Here constructors and destructors attributes

and in some cultures I create several variables of the type Tconfiguracao

conf1 :TConfiguracao;
conf2 :TConfiguracao;
...

conf1 := TConfiguracao.Create();
conf2 := TConfiguracao.Create();
...

At the close of this form I do:

FreeAndNil(conf1);
FreeAndNil(conf2);

It’s working 100%. What I’d like to know is, is there some way to go through all the objects in that class TConfiguracao and apply a FreeAndNil without knowing exactly how many variables of this type I created? I know there is a way to go through the components of a form in that way

for i := 0 to Form.ComponentCount - 1 do
  begin
    if Form.components[i].classtype is  TEdit then
    begin
      //
    end;
  end;

But objects from other classes I can’t. Is there any way to do this??

  • 1

    This is a class created by you?

  • Yes, the class was created by me.. I did not put it whole to facilitate understanding ...

2 answers

3


Assuming a class and an example type:

TMyClass = class
private
    FIndex : Integer;
public
    //Crio um novo tipo TMyClassArray que representa um array dessa nova classe.
    type TMyClassArray = array of TMyClass;  
    // Crio uma variável de nome Instances que não vai fazer parte dos objetos criados, mas sim da classe. 
    // E por isso deve ser chamada atraves do identificador da classe. 
    class var Instances : TMyClassArray; TMyClass. 
    constructor Create;
    destructor Destroy;
    //Else code...
end;

And his builder:

constructor TMyClass.Create;
begin
    inherited;
    SetLength(Instances, Length(Instances) + 1);
    FI := Length(Instances) - 1;
    Instances[FI] := Self;
    //Else code...
end;

And your destroyer:

destructor TMyClass.Destroy;
var
    I : Integer;
begin
    //Else code...
    Instances[FI] := nil;
    I := FI;
    while(I <= Length(Instances) - 1)do
    beign
        Instances[I] := Instances[I + 1];
        Instances[I].FI := I;
        Inc(I);
    end;
    SetLength(Instances, Length(Instances) - 1);
    inherited;
end;

You can access TMyClass.Instances in your code to get all instances already created.

Note 1: Maybe the destroyer needs better memory handling.

Note 2: The Convention states that Instances be a class property and not class var as I described it only by practicality.

  • I will perform some research tests and try to better understand your example.... type Tmyclassarray = array of Tmyclass; class var Instances : Tmyclassarray; I am not familiar with INSTANCES.... little OO time :( so q perform the tests put here... ja gradeco attention and help...

  • Instances is just the name I gave to variable.

  • uhmmm I get it... right.... I’m already getting access to the objects.. Now I just need to polish the Stroy...

  • In the destroy the object is already deleted from the list of instances, but does not reduce the size of the array, that will only grow with each new object of TMyClass servant.

  • that’s right... worked right here, the problem is q if it has a 3 instance position 0 - A position 1 - B position 2 - C if I remove the Indice 1 and decrease the size of the array and after that add another it will add in position 2 q is occupied by the "C"... I’m right???

  • @Felipesachetti If you just decrease yes. The right one would bring back the elements after the gap. And then decrease. I’ll edit the answer with that.

  • perfect, I made all the tests and adjustments based on the ideas you gave me, PERFECT... I only needed to make a change in relation to what you posted, before increasing the i in Tmyclass Stroy(Instances[i]). FI := i; to update the FI of instances... mto mto required even...

  • 1

    I edited with the amendment you spoke. If it worked can accept the answer. =)

Show 3 more comments

2

What you can do is use a Tobjectlist.
You must instantiate the Tobjectlist and then you can add your objects in this list through the method Add.
When you destroy Tobjectlist all objects added to it will be destroyed too.
The Ownsobjects property of Tobjectlist must be True(Default) so that it is possible to destroy the objects.
To destroy you can also use Delete, Remove, or Clear method as well.*
Note: You have to add Unit Contnrs in uses.

  • because it is @wesleyluan I even thought and I made a test with Tobjectlist the problem is that for that I would have to pass in all my formulario creating this list, because at home one of them has instances of a class with different attributes... then it would be a bit of a hassle... I ended up choosing to create an array in the class because so I only need to make the call of the instances from the standard form... q being the others Forms inherit that form.... but thanks for the tip...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.