5

Given the following declarations below, is there a way to retrieve the enum value (e.g. jt_one) from a string value (e.g. 'one')?

type
 TJOBTYPEENUM =(jt_one, jt_two, jt_three);


CONST JOBTYPEStrings : ARRAY [jt_one..jt_three] OF STRING =
     ('one','two','three');

Or do i need to create my own function using a nested set of if statements?

NOTE: I am not looking for the string "jt_one"

6
  • 5
    Is it just me or is it difficult to read these ALLCAPSIDENTIFIERS? Delphi convention would dictate TJobType =(jtOne, jtTwo, jtThree); Commented Oct 18, 2012 at 18:04
  • 1
    Sorry, but i am not sure everyone follows the convention to a tee, especially us newbees who are just learning. Maybe you can leave room for some understanding? Commented Oct 18, 2012 at 18:37
  • 1
    I cannot recall being rude to you. But text-based communication can be a bit tricky, and sometimes one might get the impression that someone is rude when he actually isn't. Commented Oct 18, 2012 at 18:40
  • 2
    @Jake I can assure you than Andreas was not being rude. He never is. We tend to be quite terse here without the niceties of speech that you would use face to face. He's just suggesting that Pascal case, WhichLooksLikeThis, is easier to read and more standard for Delphi code. Commented Oct 18, 2012 at 18:52
  • @JakeSnake I would reccomend reading this article here: cs.ut.ee/~jellen/delphi/cs.html it is very useful to writing neater pascal standard code, I find it is really helpful. Like yourself when I first started I followed no standard other than my own. If I looked at my old code now I would be shocked! In the long run it helps if it is in a universal standard, especially when posting code snippets it makes it easier for everyone to read. Commented Oct 18, 2012 at 18:58

2 Answers 2

10
function EnumFromString(const str: string): TJOBTYPEENUM;
begin
  for Result := low(Result) to high(Result) do 
    if JOBTYPEStrings[Result]=str then
      exit;
  raise Exception.CreateFmt('Enum %s not found', [str]);
end;

In real code you'd want to use your own exception class. And if you want to allow case insensitive matching, compare strings using SameText.

Sign up to request clarification or add additional context in comments.

2 Comments

@FabricioAraujo It's a pretty well known idiom. I think this is pretty much the canonical Delphi implementation of linear search.
I just found it curious, as I tend to write code much like the Andreas' samples. Looking in the docs, it's perfectly valid - as the loop is interrupted by Exit statement (if the loops completes, the value of Result is undefined).
7
function GetJobType(const S: string): TJOBTYPEENUM;
var
  i: integer;
begin
  for i := ord(low(TJOBTYPEENUM)) to ord(high(TJOBTYPEENUM)) do
    if JOBTYPEStrings[TJOBTYPEENUM(i)] = S then
      Exit(TJOBTYPEENUM(i));
  raise Exception.CreateFmt('Invalid job type: %s', [S]);
end;

or, neater,

function GetJobType(const S: string): TJOBTYPEENUM;
var
  i: TJOBTYPEENUM;
begin
  for i := low(TJOBTYPEENUM) to high(TJOBTYPEENUM) do
    if JOBTYPEStrings[i] = S then
      Exit(i);
  raise Exception.CreateFmt('Invalid job type: %s', [S]);
end;

3 Comments

This function fails to set the return value in case S is not in the array
That's better. The first version with the casting is just poor. I'd stick with your second version and just get rid of the one with the casts.
@David: Yeah, I realised that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.