Post by Sylvain Lecornétype TImageType = (IMAGE1, IMAGE2, OPTIC_FLOW);
I would like to know if there is a way to get all the posible values of a
variable TImageType (to put it into a ComboBox).
I also want to know how to assign a value to a variable of that type. For
example to asign IMAGE1 to a variable of type TImageType.
An enumerated type is effectively an integer allocated values from 0 to
type-count - 1.
By convention they are named with a prefix of the initials of the type name,
and in camel-case (however Win API constants are usually specified in
upper-case) ...
TImageType = (itImage1, itImage2, itOpticFlow);
You can use Low() and High() to get the lowest and highest values. I usually
use an array to get strings of the type, although one can access (with more
complexity) the Delphi Run-Time Type Info to obtain the name of the enumerated
value as a string.
const
ImageTypeStr = array(TImageType) of string = ('itImage1', 'itImage2',
'itOpticFlow');
// of course you could use more user friendly names in your array
// Note that an array with enumerated value indices is specified with only
the type
// Delphi knows how large it should be
var
I : TImageFlow;
begin
ComboBox.Clear;
for I := Low(TImageType) to High(TImageType) do
ComboBox1.Items.Add(ImageTypeStr[I]);
It is my convention to use lower case i, j, k, etc for loop counts which are
integers, and upper-case I, J, K, etc for loop counts which are enumerated
values to aid code clarity (but that may be thought to be idiosyncratic).
You can typecast the enumerated values to integers (and vice-versa) if you need
to, but with the usual caveats when type-casting.
So you can also store the enumerated value in the objects array of the combobox
with type-casting in and out to use a user=selected value ...
ComboBox1.AddObject(ImageTypeStr[I], TObject(I));
Enumerated types come into their own when you use them in sets (but only up to
256 values).
Alan Lloyd
***@aol.com