Solved

convenient change from type def to a string value and back

Posted on 2004-08-10
2
144 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 144
Graphics32 under Delphi 10.1 Berlin 2 128
Is there any way to treat Lock record in table with clientdataset? 3 32
IP without any Dots 1 59
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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