Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #295 String to Enum

Given the enumerated type T, create a function TryStrToEnum that takes a string s as input and converts it into an enum value of type T.

Explain whether the conversion is case sensitive or not.
Explain what happens if the conversion fails.

X : T := T'Value (S);
T.values.byName(s);
T x = T.valueOf(s);
function TryEnumToStr(const s: string; out enum: T): Boolean;
var
  code: integer;
begin
  Val(s, enum, code);
  Result := (code = 0);
end;
t = T[s]

New implementation...
< >
Bart