0

I'm working on a Delphi project that includes a number of units. Two of these units, let's call them UnitA and UnitB, each contain an initialization section. My aim is to ensure that the initialization section of UnitB is always called after UnitA's initialization.

I understand that the order of initialization in Delphi is generally influenced by the order of the uses clause in the units. However, I've found that this isn't always as clear or predictable as I would like.

What I'm wondering is whether there's a way to explicitly force or guarantee the order of initialization. For example, is there a practice or strategy I could employ that would reliably enforce that UnitB's initialization occurs after UnitA's?

In particular, if there are specific methods or techniques that help clarify and control the initialization order of units in Delphi, I would be very interested in learning about them.

zeus
  • 12,173
  • 9
  • 63
  • 184
  • The only way to enforce this is putting `UnitA` in `UnitB` uses list. – Dalija Prasnikar May 18 '23 at 12:52
  • 1
    Usually the best approach is to remove the requirement for init order to be significant – David Heffernan May 18 '23 at 13:07
  • @DalijaPrasnikar if UnitA is in UnitB do i will at 100% certain that UnitA.initialization will be call before UnitB.initialization ? – zeus May 18 '23 at 19:41
  • How about reading the linked questions and their answers? Your Q was closed neither by accident, nor to force answers in comments. – AmigoJack May 19 '23 at 01:25
  • If `UnitA` is in `UnitB` uses clause and `UnitB` does not have `UnitA` in its uses clause then order of initialization is guaranteed. Only when units reference each other there are other outside factors in play that can change the order. – Dalija Prasnikar May 19 '23 at 08:20

0 Answers0