当在其他单元中间接定义类型时,如何使用枚举值? [英] How do I use enum values when their type is defined indirectly in another unit?

查看:107
本文介绍了当在其他单元中间接定义类型时,如何使用枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试减少使用数量,并且遇到问题枚举

I am trying to reduce the number of Uses and am running into problems with Enums

(* original unit, where types are defined etc *)
unit unit1;
type
  TSomeEnumType = (setValue1, setValue2, ...);
...

(* global unit where all types are linked *)
unit unit2;
uses unit1;
type
  TSomeEnumType = unit1.TSomeEnumType;
...

(* form unit that will use the global unit *)
unit unitform;
uses unit2;
...
procedure FormCreate(Sender : TObject);
var ATypeTest : TSomeEnumType;
begin
  ATypeTest := setValue1; (* error says undeclared *)
  ATypeTest := TSomeEnumType(0); (* Works but there's not point in use enum *)
end;
...

问题是在单位形式 setValue1 表示未声明。

The problem is that in the unitform setValue1 says it is undeclared. How can I get around this?

推荐答案

您不仅可以导入类型,还可以导入常量,如下所示:

You can not only import the type, but also the constants, like so:

unit unit1;
type
  TSomeEnumType = (setValue1, setValue2, ...);
...

/* global unit where all types are linked */
unit unit2;
uses unit1;
type
  TSomeEnumType = unit1.TSomeEnumType;
const
  setValue1 = unit1.setValue1;
  setValue2 = unit1.setValue2;
  ...

请注意,如果想到最后,所有单位应使用 unit2 ,从不 unit1 ,但是您希望允许目前使用 unit1 继续编译,另一种处理方式是删除 unit1 ,将 TSomeEnumType code> unit2 直接在项目选项中,将 unit1 = unit2 放在单元别名中。每次一个单位然后使用unit1; ,它将真的拉入 unit2

Note that if the idea is that in the end, all units should use unit2 and never unit1, but you want to allow units that currently use unit1 to continue compiling, another way of dealing with that is to remove unit1, put TSomeEnumType in unit2 directly, and in your project options, put unit1=unit2 in the unit aliases. Every time a unit then does uses unit1;, it will really pull in unit2.

这篇关于当在其他单元中间接定义类型时,如何使用枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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