Lazarus

Free Pascal => General => Topic started by: pcurtis on January 28, 2022, 01:51:49 pm

Title: [SOLVED] Enumerated type
Post by: pcurtis on January 28, 2022, 01:51:49 pm
I have an enumerated type

Code: Pascal  [Select][+][-]
  1. type
  2.   TLEDShape = (lsRect, lsCircular, lsLeft, lsRight, lsUp, lsDown, lsDiamond, lsStar);
  3.  

I fill a combo box with the enum strings

Code: Pascal  [Select][+][-]
  1.   for i := 0 to GetEnumNameCount(TypeInfo(TLEDShape)) - 1 do
  2.     ComboBox1.Items.Add(GetEnumName(TypeInfo(TLEDShape), i));
  3.  

How can I convert a string back to it's enum type?

Code: Pascal  [Select][+][-]
  1. var
  2.   abc : TLEDShape;
  3.  
  4.   abc:= 'lsUp';  << Here
  5.  
Title: Re: Enumerated type
Post by: Zvoni on January 28, 2022, 01:53:57 pm
GetEnumValue?
https://www.freepascal.org/docs-html/rtl/typinfo/getenumvalue.html
Code: Pascal  [Select][+][-]
  1. var
  2.   abc : TLEDShape;
  3.  
  4.   abc:=GetEnumValue(TypeInfo(TLEDShape), 'lsUp');  << Here
  5.  

EDIT: If i read the docs right, this only works for well-formed enums (starting at 0, no "jumps" in between)
It won't work (at least as i understand it) for something like this
Code: Pascal  [Select][+][-]
  1. Type
  2.    TPrime = (One=1, Two=2, Three=3, Five=5, Seven=7, Eleven=11);
  3.  
Title: Re: Enumerated type
Post by: Thaddy on January 28, 2022, 01:54:17 pm
Either maintain a second array as string to the enumeration or use a TDictionary/TMap.

But GetEnumValue is a good suggestion.
Title: Re: Enumerated type
Post by: pcurtis on January 28, 2022, 02:15:29 pm
abc := GetEnumValue(TypeInfo(TLEDShape), 'lsUp');

doesn't compile

unit1.pas(43,10) Error: Incompatible types: got "LongInt" expected "TLEDShape"
Title: Re: Enumerated type
Post by: Zvoni on January 28, 2022, 02:16:31 pm
Code: [Select]
abc :=TLEDShape( GetEnumValue(TypeInfo(TLEDShape), 'lsUp'));
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. Uses TypInfo;
  4.  
  5. Type
  6.   TLEDShape = (lsRect, lsCircular, lsLeft, lsRight, lsUp, lsDown, lsDiamond, lsStar);
  7.  
  8. Var
  9.   abc:TLEDShape;
  10.   s:String;
  11.   i:Integer;
  12. begin
  13.   s:=GetEnumName(TypeInfo(TLEDShape),4);
  14.   Writeln(s);  //prints lsUp
  15.   i:=GetEnumValue(TypeInfo(TLEDShape),s);
  16.   Writeln(i);  //prints 4
  17.   abc:=TLEDShape(i);
  18.   Writeln(abc); //prints lsUp
  19.  
  20. end.
  21.  
  22.  
Title: Re: Enumerated type
Post by: pcurtis on January 28, 2022, 02:22:43 pm
OK. Working. Thanks.
Title: Re: Enumerated type
Post by: Zvoni on January 28, 2022, 02:27:25 pm
Just found out:
Code: Pascal  [Select][+][-]
  1. Var
  2. t:String;
  3. abc:TLEDShape;
  4. Begin
  5. abc:=lsRect;
  6. WriteStr(t,abc);
  7. Writeln(t);   //prints lsRect        !!!! No GetEnumName used?!?!?
  8. End.
  9.  
Title: Re: [SOLVED] Enumerated type
Post by: Thaddy on January 28, 2022, 05:04:09 pm
Yes, enumerations can be handled directly with writeln and also writestr.
Title: Re: [SOLVED] Enumerated type
Post by: Zvoni on January 28, 2022, 06:04:02 pm
Out of curiosity:
Is there a Type-Helper (or even built-in functionality) like this:
Taking above as example
Code: Pascal  [Select][+][-]
  1. abc:=lsUp;
  2. AnInt:=abc.ToInteger;   //AnInt being an Integer, with a value of 4 now
  3. AString:=abc.ToString;  //AString now being ‚lsUp‘
  4.  

Would conform to OOP like other types
Title: Re: [SOLVED] Enumerated type
Post by: dseligo on January 28, 2022, 06:16:33 pm
Out of curiosity:
Is there a Type-Helper (or even built-in functionality) like this:
Taking above as example

Code: Pascal  [Select][+][-]
  1. abc:=lsUp;
  2. AnInt:=Ord(abc);
  3. WriteStr(AString, abc);
  4.  
Title: Re: [SOLVED] Enumerated type
Post by: Zvoni on January 28, 2022, 10:14:56 pm
Out of curiosity:
Is there a Type-Helper (or even built-in functionality) like this:
Taking above as example

Code: Pascal  [Select][+][-]
  1. abc:=lsUp;
  2. AnInt:=Ord(abc);
  3. WriteStr(AString, abc);
  4.  

That wasn‘t my question….
Title: Re: [SOLVED] Enumerated type
Post by: avk on January 29, 2022, 08:05:48 am
Yet another version:
Code: Pascal  [Select][+][-]
  1. type
  2.   TLEDShape = (lsRect, lsCircular, lsLeft, lsRight, lsUp, lsDown, lsDiamond, lsStar);
  3. var
  4.   abc: TLEDShape;
  5.   s: string;
  6.   c: Integer;
  7. begin
  8.   s := GetEnumName(TypeInfo(TLEDShape), 4);
  9.   Val(s, abc, c);
  10.   if c = 0 then
  11.     WriteLn(abc)
  12.   else
  13.     WriteLn('Seems like something went wrong...');
  14. end.
  15.  
Title: Re: [SOLVED] Enumerated type
Post by: PascalDragon on January 30, 2022, 11:33:15 am
Is there a Type-Helper (or even built-in functionality) like this:

No, because you'd have to declare a type helper for each enum type. There is no general enum type helper just like there's no general array type helper. The built-in functionality is ReadStr, WriteStr and friends.
Title: Re: [SOLVED] Enumerated type
Post by: Zvoni on January 31, 2022, 08:44:49 am
PD,
thx for clarifying.
TinyPortal © 2005-2018