Overriding constructor in non-component class

Hi,

I would like to replace the create constructor in a subclass of TObject, like this :

MyClass = class
            SomeVar : String;
            constructor Create(A : String);
            ...
          end;

constructor MyClass.Create(A : String);
begin
  SomeVar := A;
end;

But at the assignment I get an application error, so something is wrong.  Any idea ?  TObject.Create is static, so why can't I just redefine the Create method ?

Any should I subclass TPersistent instead of TObject ?

Thanks,

Lecossois.
lecossoisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
Hi,

I assume you are instantiating your class wrong. You should also consider to use the standard convention on naming classes etc. in Delphi. I'd write the code so:

type
  TyClass = class
  private
    FSomeVar: String;
  public
    constructor Create(const A: String);
  end;

constructor TMyClass.Create(const A: String);
begin
  FSomeVar := A;
end;

The instantiation would look so:

var
  MyClass: TMyClass;

begin
  MyClass := TMyClass.Create('Hello');
end;

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MadshiCommented:
Mike is right. But please don't forget to call the inherited constructor:

constructor TMyClass.Create(const A: String);
begin
  inherited Create;
  FSomeVar := A;
end;

Regards, Madshi.
0
LischkeCommented:
Not necessary, Madshi, as TObject has no constructor.

Ciao, Mike
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

MadshiCommented:
Maybe, but do we know that there never will be a contructor in TObject in any future Delphi version? So it is surely better to go the clean way. Don't you think so? And another thing: it seems that lecossois just begins with this stuff. So if he tomorrow derives a new class from e.g. TBitmap, he will perhaps forget that inherited call, if we don't tell him today...   :-)

Regards, Madshi.
0
AttarSoftwareCommented:
In delphi 5, the reintroduce flag is cool too (not neccessarily for this case, but I just thought I would stick my oar in) ;O) :

  constructor Create( AOwner : tControl ; name : string ) ; reintroduce ;

Tim.
0
LischkeCommented:
Madshi, you would be a very good teacher :-)

Tim, I agree. There are even more cool things there. I have already used 'reintroduce' with and/or overloaded, virtual, default parameters etc. For a newbie the declaration looks more and more stranger :-)

Ciao, Mike
0
MadshiCommented:
:-)
0
rwilson032697Commented:
Lischke said: Not necessary, Madshi, as TObject has no constructor.  

Almost correct! It has one, but it does nothing :-) :-) ****, stop throwing rocks guys!

The other really good reason to always call the inherited Create is for when you change the class you derived TMyClass from TObject to TOtherObject...

Cheers,

Raymond.
0
IndefreiCommented:
Come on, Raymond
even the best can survey something
nobody is throwing rocks here
Indi
0
MadshiCommented:
Hmmm... Mike, this rock is for you!!

piuuuuuuuuuuuhhhh....  [bombbbbb]...

Sh*... I missed him...

:-)))
0
LischkeCommented:
aua :-)))
0
lecossoisAuthor Commented:
It was indeed the instantiating that was not correct.

Could you also comment on why I should use TPersistent instead of TObject ?  I read somewhere that you should do that if you would like to do assignments, but it's not clear to me what they mean by 'assignments'.

Thanks,

Lecossois.
0
LischkeCommented:
Lecossois,

I wouldn't generally say to use TPersistent. As the name vaguely implies TPersistent is a class to be stored. It therefor supports methods for streaming out properties, event handler names etc.

If you need to assigne one class to another then it might be useful to derive from TPersistent instead TObject. This is not so because you would automatically get a method which assigns a class to another one, but it primarily provides the method as such, so it can be called in an application. You have still to override TPersistent.Assign and assign all the things you need in your own method.

Ciao, Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.