Ver Mensaje Individual
  #1  
Antiguo 13-02-2009
noob noob is offline
Miembro
 
Registrado: sep 2008
Posts: 212
Reputación: 16
noob Va por buen camino
¿Cómo saber si un objeto está ya creado?

Hola, tengo una clase con un método que me sirve para inicializarla, la clase tiene como atributos otros objetos:

Código Delphi [-]
procedure MiClase.Inicializar;
begin
  Self.Atributo1 := cFoo.Create;
  Self.Atributo2 := cBar.Create;
end;

y aunque funciona siempre me gustaría hacer algo así;

Código Delphi [-]
procedure MiClase.Inicializar;
begin
  if Atributo1 ESTA CREADO then
    begin
      Self.Atributo1 := cFoo.Destroy; // o Atributo1 := cFoo.Free;
      Self.Atributo1 := cFoo.Create;
    end
  else
    Self.Atributo1 := cFoo.Create;    
  if Atributo2 ESTA CREADO then
    begin
      Self.Atributo2 := cBar.Destroy; // o Atributo2 := cFoo.Free;
      Self.Atributo2 := cBar.Create;
    end
  else
    Self.Atributo2 := cBar.Create;
end;

¿Cómo puedo saber si un objeto está ya creado? ¿Basta con hacer lo siguiente?

Código Delphi [-]
if Objeto <> nil then
  ShowMessage('El objeto está creado');

Saludos.
Responder Con Cita