Is there a problem with overriding the Delphi form constructor or using the OnCreate event?

Delphi help says either override the form constructor or use the OnCreate event. But do not do both. What is the reason for this? The only thing I can see is that if the inherited is excluded from the constructor in the child, TCustomForm.Create will not be called. Therefore, OnCreate will not be called in this case. But if the inherited is not excluded, I do not see the problem.

edit: I have to add the reason for my question. In fact, I do not plan to use both classes in the same class. But I was considering overriding the constructor in the base class when the child is already using OnCreate. So I was wondering if there was any conflict that I did not know about. But I get the impression that everything is in order. Although I can just use OnCreate in the base class so that it is consistent.

other editing: thank you all for your help. It seems that using both methods will not actually break anything if you do it right. But the problem with this is that it makes code difficult to understand. And I think I should choose the best answer, but everyone seems to agree. So Iโ€™ll just pick the one that was published first.

+6
source share
5 answers

Ultimately, you can put identical code or code for conflict in two different places. I almost always use the OnCreate event myself.

+2
source

Both will receive a call. The advice sounds though, since both of them mislead code readers.

+4
source

Given your editing, there is no problem using them.

Just make sure you write your overriden constructor as follows:

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 keep that in mind. This can be confusing quickly, so management recommends against it.

Attention

If you override the class, do not use OnCreate, because it can block users from using the OnCreate event, just complete your task in the overriden constructor. The rule is that the material, which should be the same in the Create of each TMyForm instance, must go into the override constructor.

Create material that may vary depending on where TMyForm is used should go into the OnCreate handler.

If there are any dependencies on other components nearby, then your code should always go to the OnCreate handler.

+3
source

If there are several places, you can do anything. Just pick one and stick with it. Use only another place if justified.

The main reason is clarity. You avoid some mistakes because you follow the same pattern.

+2
source

I use the onCreate event when necessary - I never like to override the default constructor TForm - IMO, at some point you will constantly encounter complications and confusion.

Other ways of handling this, which I prefer when possible:

Add the 'initialize' method to the form - call it after creation, but before , showing it. This is similar to what happens with COM objects - the constructors have no parameters, and you do your work in the Initialize call.

It is also sometimes recommended to use variables and functions in the form module, but outside the form class - as Delphi always does. In conjunction with this approach, also consider the initialization and completion sections of your form unit, albeit with GREAT CARE, especially with COM objects or components in which you use the Owner property to clean up - you will probably encounter problems with AV when shutting down -cleaning.

0
source

Source: https://habr.com/ru/post/886376/


All Articles