Enumerated types incompatible with integer types

Posted by Bill DelphiMan

In Reply to Enumerated types and incompatible type compile errors in Delphi 2007 posted by Warwick Weston Wright

: I was recently writing code using a list of 10 enumerated constants the declaration had the following code:
: Type STTypes=(ST64BitInteger,.....);

: and the following line of code caused a compile time error 'incompatible types'
: if ST64BitInteger=FrmNewTbl.FrmCreateNewTbl.CmbType.ItemIndex then ... etc;
: The question is: are enumerated types in Delphi 2007 treated as constants or is there a bug in the language because my extensive programming experience with many languages has taught me that enumerated types and constants are exactly the same thing as literal values. Or are enumerated types actually designed for a different purpose in Delphi & if so what is this purpose. Personally I think it's a bug.
-------------------

It's not a bug, it's done by design: Delphi uses a so-called "strongly typed" language, quite different from languages like Basic or scripts like JavaScript, not to mention C where almost everything seems to be allowed ;)

The compilation error has nothing to do with the fact that enumerated types are constants or not (well, they are constants, but this irrelevant here).
You get an error because you mix incompatible "types": you're trying to compare a value of the enumerated type STTypes with a value of the type "integer".

If you want to compare two values of different types, then you have to at least convert one of them to another type. Only very occasionally this conversion is done automatically by the compiler, but in most cases you have to explicitly specify the conversion.

What should work, is the following:

if FrmNewTbl.FrmCreateNewTbl.CmbType.ItemIndex = Ord(ST64BitInteger) then ...

Ord() returns the "ordinality" of the enumerated value. By default, the ordinalities start from 0. When you declare for example:

type STTypes = (ST64BitInteger, STOther, STYetAnother,...);

then the ordinality of ST64BitInteger is 0, the ordinality of STOther is 1, and so on.