Solved

convenient change from type def to a string value and back

Posted on 2004-08-10
2
145 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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