boardtc
asked on
Convert enumerated type to string generically
I want to be able to generically set Combobox.Items.CommaText to an enumarted type. So for example
type
MyEnum = (One, Two, Three)
I want to achieve
Combobox.Items.CommaText := 'One, Two, Three';
by doing
Combobox.Items.CommaText := ConvertCommaString(MyEnum );
I know I can use the TypInfo routines
function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;
function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;
Name := GetEnumName(TypeInfo(MyEnu m), Integer(Three));
Value := MyEnum(GetEnumValue(Typein fo(MyEnum) , Name));
But I want to have a gereric solutionfor converting any enumerated type. Is this possible?
Thanks, Tom.
type
MyEnum = (One, Two, Three)
I want to achieve
Combobox.Items.CommaText := 'One, Two, Three';
by doing
Combobox.Items.CommaText := ConvertCommaString(MyEnum );
I know I can use the TypInfo routines
function GetEnumName(TypeInfo: PTypeInfo; Value: Integer): string;
function GetEnumValue(TypeInfo: PTypeInfo; const Name: string): Integer;
Name := GetEnumName(TypeInfo(MyEnu
Value := MyEnum(GetEnumValue(Typein
But I want to have a gereric solutionfor converting any enumerated type. Is this possible?
Thanks, Tom.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Superb!!!! Thank you.
That looks like exactly what I am looking for.
I'll test that later.
Some of my enumerated types have commas in them. I'll work on modifying the above. Presumably a ',' needs to wrappedin quotes (').
Cheers, Tom.
That looks like exactly what I am looking for.
I'll test that later.
Some of my enumerated types have commas in them. I'll work on modifying the above. Presumably a ',' needs to wrappedin quotes (').
Cheers, Tom.
I am glad I could help.
The correction:
MyStr := MyStr + '""GetEnumName(EnumType,i) +'"';
Cheers!
...Shu
The correction:
MyStr := MyStr + '""GetEnumName(EnumType,i)
Cheers!
...Shu
ASKER
Worked a dream! Only think remaining for meto fihure out now is how to include commas in the string which I assign to commatetx.
Cheers, Tom.
Cheers, Tom.
Like you already said, wrap it with " to include , in a commatext.
Like
MyStr := MyStr + '"' + GetEnumName(EnumType,i)+ '"';
I don't know how you got an enum name to have comma!
Cheers,
...Shu
Like
MyStr := MyStr + '"' + GetEnumName(EnumType,i)+ '"';
I don't know how you got an enum name to have comma!
Cheers,
...Shu
unit EnumUnit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo;
type
MyEnum = (One, Two, Three);
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ComboBox: TComboBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender
procedure GetValues(EnumType:PTypeIn
var
I:Integer;
MyStr: String;
begin
MyStr := '';
with GetTypeData(EnumType)^ do
begin
for I:=MinValue to MaxValue do
begin
MyStr := MyStr + GetEnumName(EnumType,i);
if i < MaxValue then
MyStr := MyStr + ',';
end;
end;
ComboBox.Items.CommaText := MyStr;
end;
begin
GetValues(TypeInfo(MyEnum)
end;
end.
Cheers!
...Snehanshu