<p>How could i implement Type-Safe Enumerations in Delphi in a COM scenario ? Basically, i'd like to replace a set of primitive constants of a enumeration with a set of static final object references encapsulated in a class ? . 
In Java, we can do something like:</p>

<pre><code>public final class Enum
{
    public static final Enum ENUMITEM1 = new Enum ();
    public static final Enum ENUMITEM2 = new Enum ();
    //...
    private Enum () {}
} 
</code></pre>

<p>and make comparisons using the customized enumeration type:</p>

<pre><code>if (anObject != Enum.ENUMITEM1) ...
</code></pre>

<p>I am currently using the old Delphi 5 and i would like to declare some enums parameters on the interfaces, not allowing that client objects to pass integers (or long) types in the place of the required enumeration type. 
Do you have a better way of implementing enums other than using the native delphi enums  ? </p>

[{'answer_id': 30654, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 2, 'selected': False, 'text': '<p>What is wrong with native Delphi enums? They are type safe.</p>\n\n<pre><code>type\n  TMyEnum = (Item1, Item2, Item3);\n\nif MyEnum &lt;&gt; Item1 then...\n</code></pre>\n\n<p>Since Delphi 2005 you can have consts in a class, but Delphi 5 can not.</p>\n\n<pre><code>type\n  TMyEnum = sealed class\n  public\n    const Item1 = 0;\n    const Item2 = 1;\n    const Item3 = 2;\n  end;\n</code></pre>\n'}, {'answer_id': 37211, 'author': 'Barry Kelly', 'author_id': 3712, 'author_profile': 'https://Stackoverflow.com/users/3712', 'pm_score': 2, 'selected': False, 'text': "<p>Native Delphi enumerations are already type-safe. Java enumerations were an innovation for that language, because before it didn't have enumerations at all. However, perhaps you mean a different feature - enumeration values prefixed by their type name.</p>\n\n<p>Upcoming Delphi 2009, and the last version of the Delphi for .NET product, support a new directive called scoped enums. It looks like this:</p>\n\n<pre><code>{$APPTYPE CONSOLE}\n{$SCOPEDENUMS ON}\ntype\n  TFoo = (One, Two, Three);\n{$SCOPEDENUMS OFF}\n\nvar\n  x: TFoo;\nbegin\n  x := TFoo.One;\n  if not (x in [TFoo.Two, TFoo.Three]) then\n    Writeln('OK');\nend.\n</code></pre>\n"}, {'answer_id': 40650, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 2, 'selected': True, 'text': "<p>Now you have provided us with some more clues about the nature of your question, namely mentioning COM, I think I understand what you mean. COM can marshal only a subset of the types Delphi knows between a COM server and client. You can define enums in the TLB editor, but these are all of the type TOleEnum which basically is an integer type (LongWord). You can have a variable of the type TOleEnum any integer value you want and assign values of different enum types to each other. Not really type safe.</p>\n\n<p>I can not think of a reason why Delphi's COM can't use the type safe enums instead, but it doesn't. I am afraid nothing much can be done about that. Maybe the changes in the TLB editor in the upcoming Delphi 2009 version might change that.</p>\n\n<p>For the record: When the TLB editor is not used, Delphi is perfectly able to have interface with methods who have type safe enums as parameters.</p>\n"}, {'answer_id': 41266, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 1, 'selected': False, 'text': '<p>I think I know why Borland choose not to use type safe enums in the TLB editor. Enums in COM can be different values while Delphi only since Delphi 6 (I think) can do that. </p>\n\n<pre><code>type\n  TSomeEnum = (Enum1 = 1, Enum2 = 6, Enum3 = 80);  // Only since Delphi 6\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/30529', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2015/']