Simple, little trick with enumerations

Share your advanced PureBasic knowledge/code with the community.
RobertSF
User
User
Posts: 61
Joined: Thu May 03, 2018 4:24 pm

Simple, little trick with enumerations

Post by RobertSF »

Code: Select all

Enumeration
  #Item_1
  #Item_2
  #Item_3
  #Item_4
  #Item_5
  #Total_Entries ; automatically equals 5
EndEnumeration
If you add or remove items, #Total_Entries remains accurate. If your code needs to know the number of items, and you hardcode that number, you'll have to change it if you ever change the number of items. This way, you don't, plus your code is more readable. (e.g. If Entries_Processed = #TotalEntries vs. If Entries_Processed = #5)
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Simple, little trick with enumerations

Post by Paul »

Or just use the provided #PB_Compiler_EnumerationValue

Code: Select all

Enumeration
  #Item_1
  #Item_2
  #Item_3
  #Item_4
  #Item_5
EndEnumeration
Total_Entries=#PB_Compiler_EnumerationValue

Also keep in mind if you start your enumeration at a specified value your result won't be the number if items, but the last enumerated value that was used...

Code: Select all

Enumeration 10
  #Item_1
  #Item_2
  #Item_3
  #Total_Entries   ; value will be 13  (will not be 3)
EndEnumeration
Image Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Simple, little trick with enumerations

Post by davido »

@RobertSF,

You could try this, perhaps:

Code: Select all

dt = #PB_Compiler_Line
Enumeration 10
  #Item_1
  #Item_2
  #Item_3
  #Item_4
  #Item_5
EndEnumeration
Debug #PB_Compiler_Line - dt - 3
DE AA EB
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Simple, little trick with enumerations

Post by NicTheQuick »

This is also a nice Enumeration trick to get powers of 2:

Code: Select all

Macro enum2pow(_const_)
   _const_ = (Bool(#PB_Compiler_EnumerationValue = 0) + Bool(#PB_Compiler_EnumerationValue > 0) * 2 * (#PB_Compiler_EnumerationValue - 1))
EndMacro

Enumeration
    enum2pow(#c1)
    enum2pow(#c2)
    enum2pow(#c3)
    enum2pow(#c4)
    enum2pow(#c5)
    enum2pow(#c6)
    enum2pow(#c7)
EndEnumeration
 
Debug #c1
Debug #c2
Debug #c3
Debug #c4
Debug #c5
Debug #c6
Debug #c7
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Simple, little trick with enumerations

Post by Josh »

NicTheQuick wrote:This is also a nice Enumeration trick to get powers of 2
For this I use EnumerationBinary :mrgreen:

Code: Select all

EnumerationBinary
  #c1
  #c2
  #c3
  #c4
  #c5
  #c6
  #c7
EndEnumeration
 
Debug #c1
Debug #c2
Debug #c3
Debug #c4
Debug #c5
Debug #c6
Debug #c7
sorry for my bad english
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Simple, little trick with enumerations

Post by Paul »

Josh wrote: For this I use EnumerationBinary :mrgreen:
Nice... I totally missed this one in the manual :D
Image Image
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Simple, little trick with enumerations

Post by Josh »

Paul wrote:Nice... I totally missed this one in the manual :D
This is described in the help under Enumeration.
sorry for my bad english
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Simple, little trick with enumerations

Post by NicTheQuick »

Josh wrote:
NicTheQuick wrote:This is also a nice Enumeration trick to get powers of 2
For this I use EnumerationBinary :mrgreen:
:lol: Oh nooo :P
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
RobertSF
User
User
Posts: 61
Joined: Thu May 03, 2018 4:24 pm

Re: Simple, little trick with enumerations

Post by RobertSF »

Wow, thanks everyone! Learned stuff I didn't know.
Post Reply