Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-08

Re: How to make a constructor failed?

7 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Van Canneyt
to mili: Hello, Every one I intend to create my own class such as the following: type TMyClass =…
mili wrote 26 lines
Hello, Every one I intend to create my own class such as the following: >>>> type TMyClass = class(TObject) public constructor Create(fn:String); destructor Free; end;//TMyClass implementation constructor TMyClass.Create(fn:String); begin if not FileExists(fn) then begin // ---> Here I want to make the new object nil end; end;//TMyClass.Create demo: myclass:=TMyClass.Create('aaaa.txt'); if myclass=nil then exit;// ---> Here I can check if the object is valid <<<<<<<< Can you tell me what's the constructor should be like to implement this?

As far as I know, there is no way to do this. The best you can do is raise an exception and catch this.

Michael.

mili

Hello, Every one

I intend to create my own class such as the following:

>>>
type
  TMyClass = class(TObject)
    public
      constructor Create(fn:String);
      destructor Free;
  end;//TMyClass
implementation
constructor TMyClass.Create(fn:String);
begin
  if not FileExists(fn) then begin
    // ---> Here I want to make the new object nil
  end;
end;//TMyClass.Create
demo:
  myclass:=TMyClass.Create('aaaa.txt');
  if myclass=nil then exit;// ---> Here I can check if the object is valid
<<<<<<<<
  Can you tell me what's the constructor should be like to implement this?
  Thank you,
  mili
Yet Another Geek
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

Ingmar Tulva
to mili: Hello, Every one I intend to create my own class such as the following: type TMyClass =…

On Thu, 22 Aug 2002, mili wrote:

}constructor TMyClass.Create(fn:String);
}begin
}  if not FileExists(fn) then begin
}    // ---> Here I want to make the new object nil
}  end;
}end;//TMyClass.Create
}

You answered your question yourself: "Fail".

constructor TMyClass.Create (fn:String);
 begin
   if FileExists(fn) then Fail;
   //
 end;

AFAIK this should do exactly what needed.

By the way, destructor should not be named Free - Free is another method which calls Destroy. It is usually good idea to override TObject.Destroy.

------

Ingmar

------

"Salamanders can survive up to six hours outside a fire if regularly fed pepper" (Scamander 1927).

Aitor Santamaría Merino
to Ingmar Tulva
Ingmar Tulva wrote 18 lines
On Thu, 22 Aug 2002, mili wrote: }constructor TMyClass.Create(fn:String); }begin } if not FileExists(fn) then begin } // ---> Here I want to make the new object nil } end; }end;//TMyClass.Create } You answered your question yourself: "Fail". constructor TMyClass.Create (fn:String); begin if FileExists(fn) then Fail; // end;

And what is this "fail"? Is it a predefined identifier, similar in category to those "exit" and "break"? or would I need to define such method?

Aitor

Full_Name
to Aitor Santamaría Merino

Quoting Aitor Santamaría Merino :

And what is this "fail"? Is it a predefined identifier, similar in category to those "exit" and "break"? or would I need to define such method?

If you raise an exception at the point 'fail' happent, it will also give you much the same result...

{ define an exception class }
type
  EFileExistsException = class(Exception);

{...}

constructor TMyClass.Create(fn:String);
begin
  { raise an exception in the case that the file exists }
  if FileExists(fn) then
    raise EFileExistsException.Create('Error, File exists');
end;

{...}

...
  try
    { instantiate class }
    X := TMyClass.Create('c:\aaabbbcc.txt'); //this may fail...
    try
      { Use try..finally, be nice, protect our resources }
      //do whatever with object instance
    finally
      { remember to clean up }
      X.Free;
    end;
  except
    { handle exception, names so that we don't just trap everything...
      this code should only happen if the constructor fails, unless we reuse
      the exception class }
    on E: EFileExistsException do begin
      //handle the error in some way...
      writeln(E.Message);
      { X should still be instanciated, so we must free, but as a precaution
        use Assigned() to check that assumption }
      if Assigned(X) then X.free;
    end;
  end;
...

That's how I'd do it. It's less of a kludge than attempting to hide public members behind a fake constructor. This would probably need to be compiled in Delphi mode before anyone cries about it not working ;-)

Hope that helps, anyway,

Matt

"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." Kristian Wilson, Nintendo, Inc, 1989 -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+ G e++ h--- r+++ y+++ ------END GEEK CODE BLOCK------
Ingmar Tulva
to Aitor Santamaría Merino: And what is this "fail"? Is it a predefined identifier, similar in category to those…

On Fri, 23 Aug 2002, Aitor wrote:

}And what is this "fail"? Is it a predefined identifier, similar in }category to those "exit" and "break"?

Exactly. Borland, our honourable ancestor and inventor of these things, calls them "procedures", which is confusing, of course.

Fail can only be used inside a constructor. If I remember correctly, it somehow calls destructor of this object or class. Anyway, it deallocates the instance and causes the constructor to return Nil.

------

Ingmar

------

"Salamanders can survive up to six hours outside a fire if regularly fed pepper" (Scamander 1927).