Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert enumerated type to string generically

Posted on 2003-11-03
6
Medium Priority
?
357 Views
Last Modified: 2010-04-05
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(MyEnum), Integer(Three));
  Value := MyEnum(GetEnumValue(Typeinfo(MyEnum), Name));

But I want to have a gereric solutionfor converting any enumerated type. Is this possible?

Thanks, Tom.
0
Comment
Question by:boardtc
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:snehanshu
ID: 9676181
I guess you are looking for this:

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: TObject);
procedure GetValues(EnumType:PTypeInfo);
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
0
 
LVL 5

Accepted Solution

by:
snehanshu earned 600 total points
ID: 9676196
Well, a slight modification:

procedure TForm1.Button1Click(Sender: TObject);
Function GetValues(EnumType:PTypeInfo): String;
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;
  Result := MyStr;
end;
begin
  ComboBox.Items.CommaText := GetValues(TypeInfo(MyEnum));
end;


:-)
...Shu
0
 

Author Comment

by:boardtc
ID: 9677143
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:snehanshu
ID: 9677282
I am glad I could help.
The correction:
MyStr := MyStr + '""GetEnumName(EnumType,i)+'"';

Cheers!
...Shu
0
 

Author Comment

by:boardtc
ID: 9691355
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.
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9691443
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question