2016-07-29 35 views
8

有沒有一種方法可以將Delphi中的嵌套枚舉放入自己的命名空間中?Delphi中嵌套枚舉的名稱空間

此代碼生成一個E2004:標識符重新聲明,因爲兩個枚舉都包含「未知」。

TMyType1 = class 
public type 
    TMyType1Enum = (unknown, val1, val2); 
public 
    constructor Create(); 
    ... 
end; 

TMyType2 = class 
public type 
    TMyType2Enum = (unknown, other1, other2, other3); // causes E2004 
public 
    constructor Create(); 
    ... 
end; 

在C++中的枚舉元素的標識符均在目前存在的範圍(TMyType1 ::未知和TMyType2 ::未知)。

除了預先或後綴標識符(MyType1EnumUnknown,MyType1EnumVal1,...,MyType2Enumunknown,...),是否有可能在Delphi中實現這樣的功能?

+1

下面的答案是完美的,但是從一個編碼標準點,前綴在Delphi中通常用於枚舉。作爲一個例子,請看'TFontStyle'。它的定義如下所示:'TFontStyle =(fsBold,fsItalic,fsUnderline,fsStrikeOut)' – Graymatter

回答

10

嘗試$SCOPEDENUMS。從http://docwiki.embarcadero.com/RADStudio/en/Scoped_Enums_(Delphi)

type 
    TFoo = (A, B, Foo); 
    {$SCOPEDENUMS ON} 
    TBar = (A, B, Bar); 
    {$SCOPEDENUMS OFF} 

begin 
    WriteLn(Integer(Foo)); 
    WriteLn(Integer(A)); // TFoo.A 
    WriteLn(Integer(TBar.B)); 
    WriteLn(Integer(TBar.Bar)); 
    WriteLn(Integer(Bar)); // Error 
end; 
+0

FWIW,docwiki似乎已脫機,現在。但是可以在Delphi/RAD Studio附帶的已安裝幫助文件中找到相同的文檔。我們無法鏈接到它,從這裏。 –