Recent

Author Topic: [SOLVED] Enumerated type  (Read 3179 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 939
[SOLVED] Enumerated type
« 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.  
« Last Edit: January 28, 2022, 02:23:45 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: Enumerated type
« Reply #1 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.  
« Last Edit: January 28, 2022, 01:59:57 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 11537
Re: Enumerated type
« Reply #2 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.
Путин преступник. Россияне дезинформированы.

pcurtis

  • Hero Member
  • *****
  • Posts: 939
Re: Enumerated type
« Reply #3 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"
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: Enumerated type
« Reply #4 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.  
« Last Edit: January 28, 2022, 02:23:21 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

pcurtis

  • Hero Member
  • *****
  • Posts: 939
Re: Enumerated type
« Reply #5 on: January 28, 2022, 02:22:43 pm »
OK. Working. Thanks.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: Enumerated type
« Reply #6 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.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 11537
Re: [SOLVED] Enumerated type
« Reply #7 on: January 28, 2022, 05:04:09 pm »
Yes, enumerations can be handled directly with writeln and also writestr.
Путин преступник. Россияне дезинформированы.

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: [SOLVED] Enumerated type
« Reply #8 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
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 580
Re: [SOLVED] Enumerated type
« Reply #9 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.  

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: [SOLVED] Enumerated type
« Reply #10 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….
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

avk

  • Hero Member
  • *****
  • Posts: 588
    • my self-education project
Re: [SOLVED] Enumerated type
« Reply #11 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.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 4020
  • Compiler Developer
Re: [SOLVED] Enumerated type
« Reply #12 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.

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: [SOLVED] Enumerated type
« Reply #13 on: January 31, 2022, 08:44:49 am »
PD,
thx for clarifying.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018