Delphi如何使用其他形式的类型? [英] Delphi How to use types from other forms?

查看:93
本文介绍了Delphi如何使用其他形式的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这是一个非常新手的问题。

Sorry this is a very newbie question.

我们正在维护这个庞大的应用程序。它有5种不同的形式。我们将全局变量放在一个单位(uGlobal)中。但是我似乎无法从数据单元(uData)访问它。

We have this massive application I am doing maintenance on. It has 5 different forms. We put our global variables in one unit (uGlobal). But i can't seem to access it from the data unit (uData).

我有这个:

Unit uGlobal
type
TmyType: (alpha, beta);
...

Unit uGlobal

Stuff:  TmyType  <- error, undeclared indentifier

当我尝试将uGlobal放入uData的使用部分时,它抱怨出现了循环引用。所以,这里有点头绪。他们都在同一个项目中。这是使用BDS 2006的。

When i try to put uGlobal in the uses section of uData, it complains of a circular reference. So, kinda clueless here. They are both in the same project. This is using BDS 2006.

推荐答案

您有一个循环引用,因为您的内容在 uGlobal 想要使用 uData 中的内容,反之亦然。在大型项目中,循环引用是一个大问题,因为它们极大地增加了复杂性-如果您具有循环依赖关系,它将变得更像一个BIGGER单元。我怀疑您在将项目视为大型项目之前还有很长的路要走,更不用说大规模了。 ;)

You have a circular reference because you have things in uGlobal that want to make use of things in uData and vice-versa. Circular references are a big concern in large projects because they greatly increase complexity - if you have circular dependencies, it becomes more like one BIGGER unit. I suspect you have a long way to go before your project can be considered large, let alone "massive". ;)

您有2种可能的解决方案:

You have 2 possible solutions:


  • 使用循环依赖,使一个依赖项为 weak ,另一个为 strong

  • 应用一些重新设计以完全消除该问题。 (将内容分解成较小的块。)

  • Go with the circular dependency, making one dependency weak, and the other strong.
  • Apply a little redesign to eliminate the problem alogether. (Break things into smaller chunks.)

大卫已经给出了答案:至少一个单位必须使用实施部分中的另一个单位。

David has already given the answer: At least one of the units must use the other from the implementation section.


  • A使用B使用A是绝对不允许,但是您可以将接口和实现部分看作是类似于单独的单元本身(带有一堆特殊的引用规则)。

  • 因此问问自己每个子单元需要什么?

  • 例如:

    • 您声明了一个类型在 uData接口中引用,并在 uGlobal接口中引用,然后在 uGlobal 需要 uData ,并需要一个相应的uses子句。

    • 如果其中有一个类型在任何 uGlobal 中引用的 uData实现,然后在 uData实现必须移到接口部分。

    • 如果 uGlobal接口中存在类型,则必须仅从 uData ,则在执行部分可以使用use子句。

    • A uses B uses A is absolutely not allowed, but you can think of the interface and implementation sections as being almost like separate units themselves (with a bunch of special referencing rules).
    • So ask yourself what does each sub-unit need?
    • If for example:
      • You declared a type in uData interface and referenced that in uGlobal interface, then the interface of uGlobal needs uData and will need a corresponding uses clause.
      • If there's a type in uData implementation that's referenced in anywhere uGlobal, then that declaration in uData implementation must be moved to the interface section.
      • If there's a type in uGlobal interface must it's only referenced from the implementation section of uData then that uses clause will be fine in the implementation section.

      删除循环依赖性需要将您的单元分解为更易于管理的较小单元。为此,您必须了解应用程序中每个事物之间的 依赖性
      例如:

      Removing the circular dependency requires breaking your units down into smaller ones that are more manageable. To do this, you must understand the dependencies between each of the things in your application. For example:


      • 假设 uGlobal 声明A和C

      • A取决于C,但是C不需要A

      • 还假定 uData 声明B需要C

      • 但是事实证明A也需要B

      • 这就是为什么您具有循环依赖项

      • Suppose uGlobal declares A and C
      • A depends on C, but C doesn't need A
      • Suppose also that uData declares B which needs C
      • But it turns out that A also needs B
      • This is why you have the circular dependency

      在这种情况下,您要做的就是在其中的C动作中声明一个新单位。

      All you have to do in this case is declare a new unit at move C in there.


      • 然后 uGlobal uData 都将使用 uNewUnit

      • 但两者都不需要

      • ,您的循环依赖项将被完全删除。

      • Then both uGlobal and uData will use uNewUnit
      • But neither will need the other
      • And your circular dependency will be removed entirely.

      我绝不主张您使用uGlobal。实际上,这是一个非常糟糕的主意,当您的项目开始变大时,它将在两个方面占用您大量时间。不幸的是,这种解释本身就是一个庞然大物。

      I am not in any way advocating your approach with uGlobal. In fact it is a very bad idea and will bite you big time on 2 fronts when your projects starts to get large. Unfortunately the explanation is a mammoth answer in itself.


      • 使用全局变量非常危险,应避免。

      • 使用诸如uGlobal和uData之类的大型转储单位也是危险的,您只是饱受了他们的困扰。

      这篇关于Delphi如何使用其他形式的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆