How to manage circular references in delphi units?

情到浓时终转凉″ 提交于 2019-12-01 06:04:45

问题


I am using the BeforSignup in the AfterSignup unit in order to be able to call the email variable from within AfterSignup code, finally i enbcountred a problem because i want to make a button which opens the AfterSignup window using the code :

 AfterSignup.Show;

But the problem is that i am obliged to add the AfterSignup unit to the uses list of BeforeSignup and that is exactly what i can't do because i am alreadey using BeforeSignup to AfterSignup unit.

i receive an error saying, a reference of circular unit.


回答1:


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.



回答2:


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.



来源:https://stackoverflow.com/questions/8592334/how-to-manage-circular-references-in-delphi-units

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