[fpc-pascal]Re: How to make a constructor failed?

Yet Another Geek yetanothergeek at yahoo.com
Fri Aug 23 12:05:26 CEST 2002


program createif;
{$MODE OBJFPC}

uses sysutils;

type TMyClass = class(TObject)
  private
    fFileName:string;
  public
    constructor Create(FN:string; dummy:byte);
    class function Create(FN:string):TMyClass;
end;

constructor TMyClass.Create(FN:string; dummy:byte);
begin
  inherited Create;
  fFileName:=FN;
end;

class function TMyClass.Create(FN:string):TMyClass;
begin
  if FileExists(FN) 
  then Result:=TMyClass.Create(FN, 0)
  else Result:=nil;
end;


var 
  myclass:TMyClass;

begin
  myclass:=TMyClass.Create('aaaa.txt');
  if myclass=nil then begin
    writeln('object not created - file not found');
    exit;
  end else begin
    writeln('Sucess!!!');
    myclass.Free;
  end;
end.


__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com




More information about the fpc-pascal mailing list