Francisco Reyes wrote:
Michael Van Canneyt writes:

Both Turbo Pascal's Object Pascal and Delphi's object pascal are supported, depending on which mode you compile in.

If I have a class like:
program myclass;
type
  THelloWorld = class
    procedure Put;
  end;
var
  HelloWorld: THelloWorld;
procedure THelloWorld.Put;
begin
  WriteLn('Hello, World!');
end;
begin
  HelloWorld := THelloWorld.Create;
  HelloWorld.Put;
  HelloWorld.Free;
end.


How do I use that class in another program?

You can:

uses
  UnitWithHello;

...

var
  VHello: THelloWorld;
begin
  VHello := THelloWorld.Create;
  try
    VHello.Put;
  finally
    VHello.Free;
  end;


or you can:

unit UnitWithHello;

type
  THelloWorldClass = class of HelloWorld;
  THelloWorld = class
    procedure Put; virtual; abstract;
  end;

...

unit AnotherUnit;

uses
  UnitWithHello;

...

procedure DoHello(AHelloClass: THelloWorldClass);
var
  VHello: THelloWorld;
begin
  VHello := AHelloClass.Create;
  try
    VHello.Put;
  finally
    VHello.Free;
  end;


or you can:

uses
  UnitWithHello;

type
  THelloWrapper = class
  private
    FHello: THelloWorld;
    function GetHello: THelloWorld;
  protected
    function InternalHelloClass: THelloWorldClass; virtual; abstract;
    property Hello: THelloWorld read GetHello;
  public
    destructor Destroy; override;
  end;

...

destructor THelloWrapper.Destroy;
begin
  FHello.Free;
  inherited;
end;

function THelloWrapper.GetHello: THelloWorld;
begin
  if not Assigned(FHello) then
    FHello := InternalHelloClass.Create;
  Result := FHello;
end;


or you can:

uses
  UnitWithHello;

type
  THelloWrapper = class
  private
    FHello: THelloWorld;
    FHelloClass: THelloWorldClass;
    function GetHello: THelloWorld;
 protected
    property Hello: THelloWorld read GetHello;
  public
    constructor Create(AHelloClass: THelloWorldClass);
    destructor Destroy; override;
  end;

...

constructor THelloWrapper.Create(AHelloClass; THelloWorldClass);
begin
  Assert(Assigned(AHelloClass), 'error');
  inherited Create;
  FHelloClass := AHelloClass;
end;

destructor THelloWrapper.Destroy;
begin
  FHello.Free;
  inherited;
end;

function THelloWrapper.GetHello: THelloWorld;
begin
  if not Assigned(FHello) then
    FHello := FHelloClass.Create;
  Result := FHello;
end;


And so on.

--
Joao Morais
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to