Discussion:
Delphi enum type
(too old to reply)
Sylvain Lecorné
2004-06-19 19:10:13 UTC
Permalink
Hye,

I have done a type like that:
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.

Thanks,

Sylvain.
NoRemorse
2004-06-19 19:36:50 UTC
Permalink
Post by Sylvain Lecorné
Hye,
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.
Thanks,
Sylvain.
For the first part, you might have to declare a string array containing the
captions of the values:
const ImageTypeStrings: array [TImageType] of string = ('Image 1', 'Image
2', 'Optic Flow');

Then in your code you can access the strings like this:
ImageTypeStrings[IMAGE1] yields 'Image 1'
ImageTypeStrings[OPTIC_FLOW] yields 'Optic Flow' and so on

For the second part of your post you can do the following:
var SomeVar: TImageType;
SomeVar := IMAGE1;
--
NoRemorse
"Expect me when you see me."
AlanGLLoyd
2004-06-19 20:33:19 UTC
Permalink
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
Jud McCranie
2004-06-19 20:57:29 UTC
Permalink
On Sat, 19 Jun 2004 14:10:13 -0500, "Sylvain Lecorné"
Post by Sylvain Lecorné
Hye,
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).
There is a nice function to get the name of an enumerated type as a
string:

uses system, TypInfo;

var WhichImage : tImageType;
s : string;
...

for WhichImage := low( tImageType) to high( tImageType) do
begin
s := GetEnumName( TypeInfo( tImageType), ord( WhichImage));

// add s to the combo box
...
end;

---
Replace you know what by j to email
Duncan McNiven
2004-06-20 10:28:43 UTC
Permalink
On Sat, 19 Jun 2004 14:10:13 -0500, "Sylvain Lecorné"
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).
var
ImgType : TImageType;
begin
ComboBox1.Items.Clear;
for ImgType := low(ImgType) to high (ImgType) do
ComboBox1.Items.Add(GetEnumName(TypeInfo(TImageType),ord(ImgType)));
Post by Sylvain Lecorné
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.
The simple answer is:

var
ImgType : TImageType;
begin
ImgType := OPTIC_FLOW;


What you probably want to do, though, is to assign the value that the
user selects from the combo box:

var
ImgType : TImageType;
begin
ImgType := TImageType(ComboBox1.ItemIndex);
--
Duncan
AlanGLLoyd
2004-06-20 19:07:02 UTC
Permalink
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

Loading...