PeterLarsen
asked on
About "Enumerated types"
Sample :
procedure xxxxx...
type
Tet = (MyFirst, MySecond, MyThird);
var
ET : Tet;
begin
ET:=MySecond;
label1.Caption:=inttostr(o rd(ET)); //this is no problem
label1.Caption:=ET; //this is a problem..
end;
I would like to write the string "MySecond" in the labels caption. Is it possible to do that - and how ??
Object Inspector does something like this - i just dont know how.
Regards
Peter
procedure xxxxx...
type
Tet = (MyFirst, MySecond, MyThird);
var
ET : Tet;
begin
ET:=MySecond;
label1.Caption:=inttostr(o
label1.Caption:=ET; //this is a problem..
end;
I would like to write the string "MySecond" in the labels caption. Is it possible to do that - and how ??
Object Inspector does something like this - i just dont know how.
Regards
Peter
The Members of Tet are only known by name to the compiler. After compiling they are only numbers (0, 1, 2).
As Epsylon said they are only numbers...
so something like this would be the only way to solve it?
If ET = MySecond then Label1.Caption := 'MySecond';
I suppose..
Good Luck
//raidos
so something like this would be the only way to solve it?
If ET = MySecond then Label1.Caption := 'MySecond';
I suppose..
Good Luck
//raidos
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi
or :
procedure xxxx ;
type
Tet = (MyFirst, MySecond, MyThird);
const
TetStrings : array [Tet] of string = ('MyFirst','MySecond','MyT hird');
var
ET : Tet;
begin
ET:= MyThird;
label1.Caption:= TetStrings[ET] ;
end;
or :
procedure xxxx ;
type
Tet = (MyFirst, MySecond, MyThird);
const
TetStrings : array [Tet] of string = ('MyFirst','MySecond','MyT
var
ET : Tet;
begin
ET:= MyThird;
label1.Caption:= TetStrings[ET] ;
end;
ASKER
Thanks Walter.
But it is only possible to do that if the Enum type is public or published, right ?
Thanks to you all.
Peter
But it is only possible to do that if the Enum type is public or published, right ?
Thanks to you all.
Peter
Yes I believe that is correct.
Walter
Walter
ASKER
I need one more information.
How do i get the count from the enum type, like :
For i:=0 to high(Tet)-1 do
something
Thanks
Peter
How do i get the count from the enum type, like :
For i:=0 to high(Tet)-1 do
something
Thanks
Peter
Peter
If you've created an enumerated type then you already know its count or max value so why not why not create a constant and give it the appropriate value:
Type
TNumbers = (One, Two, Lots, Many);
Const
Max_Numbers = 4;
Then you could use:
for C := 0 to Max_Numbers - 1 do
// something
or you could add an extra "dummy" value to your type i.e.:
Uses
TypInfo;
Type
TNumbers = (One, Two, Lots, Many, Max_Numbers)
Then you could use:
for C := 0 to GetEnumValue(TypeInfo(TNum bers), 'Max_Numbers') - 1 do
// something
Using GetEnumValue like this will return the number of "real" values in your type.
Is this any use?
Walter
If you've created an enumerated type then you already know its count or max value so why not why not create a constant and give it the appropriate value:
Type
TNumbers = (One, Two, Lots, Many);
Const
Max_Numbers = 4;
Then you could use:
for C := 0 to Max_Numbers - 1 do
// something
or you could add an extra "dummy" value to your type i.e.:
Uses
TypInfo;
Type
TNumbers = (One, Two, Lots, Many, Max_Numbers)
Then you could use:
for C := 0 to GetEnumValue(TypeInfo(TNum
// something
Using GetEnumValue like this will return the number of "real" values in your type.
Is this any use?
Walter
ASKER
Thanks,
It is very useful to calculate the 'count' at run-time, because the enum declaration may be created by one programmer and used by another.
Also it is very easy to forget to change a constant.
Regards
Peter
It is very useful to calculate the 'count' at run-time, because the enum declaration may be created by one programmer and used by another.
Also it is very easy to forget to change a constant.
Regards
Peter
Peter
Yes I agree about forgetting to update the const value.
Is the other option acceptable, if not I have another suggesstion. In the TypInfo unit source there is function called GetTypeData which is not in the interface section. Its very short (only 3 asm instructions) I copied this and ran it against an enum type then looked at the returned PTypeData.MaxValue and you get the maximum value of that type! Have a try, see what you think.
As a matter of interest I've used this type enumeration stuff to "use strings" in a case statement, I got the details from an old copy of The Delphi Magazine, can't remember which month.
Uses
TypInfo;
Type
TNumbers = (One, Two, Lots, Many);
Procedure xxxxxx
begin
// Get a string from somewhere
case TNumbers(GetEnumValue(Type Info(TNumb ers), aString)) of
One: ListBox1.Items.Add('One');
Two: ListBox1.Items.Add('Two');
Many: ListBox1.Items.Add('Many') ;
Lots: ListBox1.Items.Add('Lots') ;
else ListBox1.Items.Add('Eh?');
end;
end;
Cheers - Walter
Yes I agree about forgetting to update the const value.
Is the other option acceptable, if not I have another suggesstion. In the TypInfo unit source there is function called GetTypeData which is not in the interface section. Its very short (only 3 asm instructions) I copied this and ran it against an enum type then looked at the returned PTypeData.MaxValue and you get the maximum value of that type! Have a try, see what you think.
As a matter of interest I've used this type enumeration stuff to "use strings" in a case statement, I got the details from an old copy of The Delphi Magazine, can't remember which month.
Uses
TypInfo;
Type
TNumbers = (One, Two, Lots, Many);
Procedure xxxxxx
begin
// Get a string from somewhere
case TNumbers(GetEnumValue(Type
One: ListBox1.Items.Add('One');
Two: ListBox1.Items.Add('Two');
Many: ListBox1.Items.Add('Many')
Lots: ListBox1.Items.Add('Lots')
else ListBox1.Items.Add('Eh?');
end;
end;
Cheers - Walter
ASKER
Thanks Walter,
Well, i have chosen the constant solution - until a better one is found.
I know it is possible to get the 'count' somewhere - or i think it's possible. Otherwise i dont understand how the Object Inspector is capable of showing the enum type.
Also take a look at Inthe's sample :
type
Tet = (MyFirst, MySecond, MyThird);
const
TetStrings : array [Tet] of string = ('MyFirst','MySecond','MyT hird');
"Array [Tet] of string".... Somehow the 'count' is passed to the array.
-i like your case-of sample - very nice :-)
Peter
Well, i have chosen the constant solution - until a better one is found.
I know it is possible to get the 'count' somewhere - or i think it's possible. Otherwise i dont understand how the Object Inspector is capable of showing the enum type.
Also take a look at Inthe's sample :
type
Tet = (MyFirst, MySecond, MyThird);
const
TetStrings : array [Tet] of string = ('MyFirst','MySecond','MyT
"Array [Tet] of string".... Somehow the 'count' is passed to the array.
-i like your case-of sample - very nice :-)
Peter
ASKER
Hi Walter,
>> or you could add an extra "dummy" value to your type i.e.:
Your "dummy" idea is ok, but if it is for use in published properties, it will confuse the programmer - i think.
But i think i will consider using this method in private properties.
Peter
>> or you could add an extra "dummy" value to your type i.e.:
Your "dummy" idea is ok, but if it is for use in published properties, it will confuse the programmer - i think.
But i think i will consider using this method in private properties.
Peter
Peter
I made a mistake in me last comment the function GetTypeData is in the interface section of the TypInfo unit.
My guess is that a count might not available but rather the maximum value is the key. Since enum types are always ordinal values 0, 1, 2, ... maxvalue then the count of elements is maxvalue + 1.
Therefore the following will work:
Uses
TypInfo;
Type
TNumbers = (One, Two, Lots, Many);
procedure xxx;
var
C: word;
begin
for C := 0 to PTypeData(GetTypeData(Type Info(TNumb ers))).Max Value do
ListBox1.Items.Add(GetEnum Name(TypeI nfo(TNumbe rs), C));
end;
Thanks - Walter
I made a mistake in me last comment the function GetTypeData is in the interface section of the TypInfo unit.
My guess is that a count might not available but rather the maximum value is the key. Since enum types are always ordinal values 0, 1, 2, ... maxvalue then the count of elements is maxvalue + 1.
Therefore the following will work:
Uses
TypInfo;
Type
TNumbers = (One, Two, Lots, Many);
procedure xxx;
var
C: word;
begin
for C := 0 to PTypeData(GetTypeData(Type
ListBox1.Items.Add(GetEnum
end;
Thanks - Walter
ASKER
I have tried the sample from your latest comment and it's perfect.
Thanks
Peter
Thanks
Peter
Your welcome.
Walter
Walter