Solved

convenient change from type def to a string value and back

Posted on 2004-08-10
2
141 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
ID: 11763251
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
ID: 11763281
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

15 Experts available now in Live!

Get 1:1 Help Now