Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Acceder a objetos creados en tiempo de ejecución (https://www.clubdelphi.com/foros/showthread.php?t=94944)

Neftali [Germán.Estévez] 25-11-2020 15:46:36

Cita:

Empezado por newtron (Mensaje 539237)
...pero ahora se me ocurre que me vendría bien poder buscar dentro de la lista por el nombre del objeto pero no consigo dar con la sintaxis


Bueno, por eso hemos comentado lo del TStringList, porque te permite almacenar una cadena asociada a cada objeto.
Como deriva de TStrings, también posee el método IndexOf o el método Find (si está ordenada), que funcionan de la misma manera.

newtron 25-11-2020 17:04:19

:o ¡¡Andalaleche!! Y yo pensando en la manera de hacer esa búsqueda siendo un TObject.


Gracias a todos y un saludo.

movorack 25-11-2020 17:29:57

También puedes usar un TDictionary o crear tu propio objeto basado en este.

Código Delphi [-]
type
  TListaObjetos = class(TDictionary<String,Tobject>)
  public
    function Conteo: string;
  end;

function TListaObjetos.Conteo: string;
begin
  Result := Format('Tengo %d elementos', [Self.Count]);
end;

procedure TForm1.ListarObjetos(Sender: TObject);
  const
    //Algunos nombres están repetidos
    Nombres : Array [0..7] of string = ('Obj1', 'Obj2', 'Obj3', 'Obj1', 'Obj4', 'Obj2', 'Obj5', 'Obj1');
  var
    i: integer;
    ListaObjetos: TListaObjetos;
    Nombre: string;
begin
  ListaObjetos := TListaObjetos.Create;
  try
    for i := Low(Nombres) to High(Nombres) do
    begin
      Nombre := Nombres[i].Trim;

      if Nombre.IsEmpty or ListaObjetos.ContainsKey(Nombre.ToUpper) then
        Continue;

      ListaObjetos.Add(Nombre.ToUpper, TObject.Create);
    end;

    //En este punto se deben tener 5 objetos en la lista
    ShowMessage(ListaObjetos.Conteo);
  finally
    ListaObjetos.Free;
  end;
end;

newtron 25-11-2020 18:05:42

Gracias compañero. Ya tengo montado todo el tema con el TStringList.


Saludos


La franja horaria es GMT +2. Ahora son las 18:24:05.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi