问题现象:同事问我个问题,说同一个枚举类型名,在多个单元中出现,怎么指定?我说用点引用,比如说“Tmytype.My1”,可是在D2007报红。
问题原因:可能是因为我用XE时间长了,把D7的老特性给忘了。
问题处理:如果是老版本还是用老办法吧,新版本还是好用的。
实例代码(枚举类型):
![](https://img7.php1.cn/3cdc5/c8b2/4aa/516ea8ed7b3851f4.gif)
![](https://img7.php1.cn/3cdc5/c8b2/4aa/dad12a8788de42fa.gif)
1 unit Unit2;
2
3
4 interface
5
6 type
7 TMyType = (My1,My2,My3);
8
9 implementation
10
11 end.
实例代码(调用单元):
![](https://img7.php1.cn/3cdc5/c8b2/4aa/516ea8ed7b3851f4.gif)
![](https://img7.php1.cn/3cdc5/c8b2/4aa/dad12a8788de42fa.gif)
1 unit Unit1;
2
3 interface
4
5 uses
6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 procedure Button1Click(Sender: TObject);
13 private
14 { Private declarations }
15 public
16 { Public declarations }
17 end;
18
19 var
20 Form1: TForm1;
21
22 implementation
23 uses
24 Unit2;//引用枚举类型所在单元
25 {$R *.dfm}
26
27 procedure TForm1.Button1Click(Sender: TObject);
28 var
29 tMy: TMyType; //定义一个类型变量
30 begin
31 tMy := Unit2.My1; //XE10.2 D7都支持
32 tMy := Tmytype(0); //XE10.2 D7都支持
33 tMy := Tmytype.My1; //XE10.2支持 D2007 D7不支持
34 tMy := Tmytype(My1);//XE10.2支持,但我感觉没有意义
35 end;
36
37 end.