Search code examples
delphiprocedure

Delphi Using unit procedure on form


I'm learning OOP and I have created a basic program so far. I have create my own clas:

Type
Zombie = class
  private
   fLife : Integer;
   fAge : Integer;
   fHeight: String;
  public
   Constructor Create(pLife, pAge : Integer; pHeight : String);
   Procedure SetLife(pLife : Integer);
   function GetLife : Integer;
   Procedure ShowLife;
end;

The procedure ShowLife does exactly what it says:

procedure Zombie.ShowLife;
begin
ShowMessage(inttostr(fLife));
end;

I'm trying to call this procedure on a Form but it says undeclared identifier:

procedure Tform1.ShowLifebtnClick(Sender: TObject);
begin
Zombies_Unit.ShowLife;
end;

I have included the unit in the user of the Form. How can I use methods on another form


Solution

  • You need to create and free the object before/after you use it. The pattern is like this:

    MyZombie := TZombie.Create(10, 20, 30); 
    try
      MyZombie.ShowLife(); 
    finally
      MyZombie.Free();
    end;