Reputation: 27276
First of all, I doubt this is even possible, but if it is, I'd love to know how. I recall seeing this behavior before, but it could be explicitly implemented in the IDE.
I have a base form which I then inherit into various other forms. In the base form, I have a number of virtual methods which are to be overridden and implemented by the inherited forms.
There are certain virtual methods which expect inherited
to be called at the end of the procedure. However, by default, it gets automatically inserted at the beginning. This tends to cause confusion, specifically forgetting to call it at the end instead of the beginning.
How could I force inherited
to get inserted at the end instead of the beginning, like below, if at all possible?
procedure TMyForm.DoStuff;
begin
inherited;
end;
Upvotes: 3
Views: 318
Reputation: 31403
To add to David's answer, if you need to enforce this order of operations the best way to do it is by changing the design. Decoupling the operations which need to happen afterwards can be done something like this :
type
TMyBase = class
protected
DoBeforeSomething : procedure; virtual; abstract;
public
DoSomething : procedure;
end;
TMyCustom = class(TMyBase)
protected
DoBeforeSomething : procedure; override;
end;
procedure TMyBase.DoSomething;
begin
DoBeforeSomething;
// Do common things now...
end;
procedure TMyCustom.DoBeforeSomething;
begin
inherited;
// some custom things...
end;
Here the consumer would always call the DoSomething
method to perform whatever operation is intended but the inheriting class would override DoBeforeSomething
, which is called first in DoSomething
before whatever other operations need to be done.
This way the descendent class doesn't have to worry about overriding DoSomething
itself and calling inherited
in the "wrong" place, the design of the class takes care of getting the operations in the correct order.
Upvotes: 3
Reputation: 612993
You can't change this behaviour of the IDE.
The IDE knows that certain methods, e.g. overridden destructors, expect the inherited
statement to appear at the end of the method body. For such methods, the IDE does indeed place the call to inherited
at the end of the method body when performing class completion. But there is no mechanism for you to tell the IDE that other methods, about which it knows nothing, are to be treated that way.
Upvotes: 6