Solved

convenient change from type def to a string value and back

Posted on 2004-08-10
2
140 Views
Last Modified: 2012-05-05
i'm looking for a clever solution for this task convert myModes to a String Value (and convert  it back to a mymode value);
below my current approach to this task


type myModes = (all, none, ever, today, now, yesterday);

function MyMode2Str( value : myModes) :  String;
begin
      if MyMode=all then result='all';
      if MyMode=none  then result='none';
      ...
      ...
end;


function AnyStr2MyMode(value : String) : myMode;
begin

      if MyMode='all' then result=all;
      if MyMode='none'  then result=none;
      ...
      ...

end
0
Comment
Question by:BdLm
  • 2
2 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 50 total points
Comment Utility
Personally, I just like to use a string array constant for these things...

type TmyModes = (all, none, ever, today, now, yesterday);
const sMyModes: array[TmyModes] of string = ('all', 'none', 'ever', 'today', 'now', 'yesterday');

Converting to string is simple. For the other direction, just do a lookup...

var
  MyString: string;
  MyMode:TMyModes;
begin
  // To string...
  MyString := sMyModes[MyMode];
  // To TMyModes...
  MyMode := High(TMyModes);
  while (MyMode > Low(TMyModes)) and not AnsiSameText(MyString, sMyModes[MyMode]) do dec(MyMode);
end;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
Keep in mind that if you look for a non-existing string value, my method will return All as the MyMode value, not something invalid. You could do a check with  AnsiSameText(MyString, sMyModes[MyMode]) afterwards to make sure you found a valid value.

There is an alternative method using the runtime information (RTTI) but it's a bit complex. This method is the simplest one.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now