Link to home
Start Free TrialLog in
Avatar of BdLm
BdLmFlag for Germany

asked on

convenient change from type def to a string value and back

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
ASKER CERTIFIED SOLUTION
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.