Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Type-safe COM enumerations?

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:

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

and make comparisons using the customized enumeration type:

if (anObject != Enum.ENUMITEM1) ...

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 ?

like image 944
Gustavo Avatar asked Feb 03 '26 22:02

Gustavo


1 Answers

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.

Upcoming Delphi 2009, and the last version of the Delphi for .NET product, support a new directive called scoped enums. It looks like this:

{$APPTYPE CONSOLE}
{$SCOPEDENUMS ON}
type
  TFoo = (One, Two, Three);
{$SCOPEDENUMS OFF}

var
  x: TFoo;
begin
  x := TFoo.One;
  if not (x in [TFoo.Two, TFoo.Three]) then
    Writeln('OK');
end.
like image 76
Barry Kelly Avatar answered Feb 06 '26 13:02

Barry Kelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!