1

我正在尝试减少数量Uses并遇到问题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;
...

问题是在unitform中setValue1说它是未声明的。我怎样才能解决这个问题?

4

1 回答 1

5

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

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;
  ...

请注意,如果最终的想法是,所有单元都应该使用unit2and never unit1,但是您希望允许当前使用unit1的单元继续编译,另一种处理方法是删除,直接unit1放入,并在您的项目选项中,输入单位别名。每次一个单位这样做,它就会真正拉进来。TSomeEnumTypeunit2unit1=unit2uses unit1;unit2

于 2013-06-04T19:35:29.657 回答