Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert enumerated type to string generically

Posted on 2003-11-03
6
Medium Priority
?
351 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:tomcorcoran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:tomcorcoran
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:tomcorcoran
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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