Is there a problem with both overriding a Delphi form constructor, _and_ using it's OnCreate event?
Delphi help says either override a form's constructor, or use the OnCreate event. But don't do both. What's the reason for this? The only thing I can see, is if inherited is left out of the constructor in a descendant, TCustomForm.Create won't get called. So OnCreate won't get c开发者_运维百科alled in that case. But if inherited isn't left out, I don't see the problem.
edit: I should add the reason for my question. I'm not actually planning to use both in the same class. But I was considering overriding the constructor in a base class, when a descendant is already using OnCreate. So I was wondering if there was some kind of conflict I wasn't aware of. But I'm getting the impression that should be ok. Although I may just use the OnCreate in the base class to keep it consistent.
another edit: Thanks everyone for the help. So it looks like using both methods won't actually break anything, if you do it correctly. But the problem with doing it, is that it makes the code hard to understand. And I guess I'm supposed to pick a best answer, but everyone seems to be in agreement. So I'll just pick the one that was posted first.
Both will get called. The advice is sound though since using both is confusing to readers of the code.
Considering your edit, no there is no problem in using them both.
Just make sure you write your overriden constructor like so:
constructor TMyForm.Create(AOwner: TControl);
begin
inherited;
....
your new code here
end;
Also note that the OnCreate
handler will be called before your added Create code so be aware of that. This can get confusing fast, that's why the manual recommends against it.
Warning
If you override a class do not use OnCreate, because that might block users of the OnCreate event from acting on that, just do your thing in the overriden constructor. The rule is that stuff that needs to the same in the Create of every instance of TMyForm should go into an override constructor.
Create stuff that can be different depending on where the TMyForm is used should go into the OnCreate Handler.
If there are any dependencies with other components nearby, then your code should always go into the OnCreate handler.
You may end up placing identical - or comflicting - code in two different places. I almost always use the OnCreate event myself.
If there are several places you can do anything. Just chose one and stick to that. Only use another place if its justified.
The prime reason is clarity. You avoid some mistakes because you are following the same pattern.
I use onCreate event when necessary - I never like to override TForm's default constructor - IMO you'll invariably run into complications and confusion at some point further on.
Other ways of handling this, which I prefer when it's possible:
Add an 'initialize' method to the form - call it after creating it but before showing it. This akin to what happens with COM objects - constructors have no parameters and you do your business in the Initialize call.
Also sometimes it's appropriate to use variables and functions on your form unit but outside the form class - as Delphi itself continually does. In conjuction with this approach, consider using the initialization and finalization sections of your form unit as well - albeit with GREAT CARE, particularly with COM objects or components where you're using the Owner property for clean up - you're likely to run into problems with AV's on shutdown-clean ups.
精彩评论