How to manage circular references in delphi units?

孤人 提交于 2019-12-01 08:33:13

The easy solution would be to add unitA in the uses clause of the interface section of unitB and unitB in the uses clause of the implementation section of unitA

The better solution would be to break the dependency for both (or at least one) units.
You can break the dependency by either

  • moving all calls from unitA to unitB into unitB
  • adding a third, shared code unit, using both units A & B.

If your type declarations and variable declarations in the interface section of a unit require classes or variables in another unit, then you should be adding the required unit to the uses clause in the interface section.

However, if you only require the classes and variables in the implementation part of your unit, in implementation code, then you should be adding the required unit only to the uses clause in your implementation section.

If you have the unit already referenced in the interface section, you must not include it again in the implementation section.

If two units use each other only in the implementation section, there is no circular reference. You can also mix the uses, as long as two units don't use each other in the interface section (directly or indirectly), you won't have a circular reference.

So, based on your question, it seems like you should be using the units only in your implementation section, and there will be no circular reference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!