Page 1 of 2
[Implemented] Enumeration enhancements
Posted: Fri Jul 24, 2009 11:44 am
by Michael Vogel
I'm quite sure, most of these things have already been posted, but must be already a long time ago (or I just used the wrong keywords when seeking for these entries

)...
I like the possibilities to do
Enumerations, but sometimes I'd like to have some more possibilities, like the following: I want to split long constant lists into more smaller areas for structuring, what about a command which continues enumeration beginning with the highest enumerated constant so far?
Code: Select all
Enumeration
#A; 0
#B; 1
#C; 2
EndEnumeration
Enumeration Continue
#D; 3
#E; 4
#F; 5
EndEnumeration
Or what about a binary enumeration for flags?
Code: Select all
Enumeration Flag 1
#X; =1
#Y; =2
#Z; =4
EndEnumeration
[/code]
Posted: Fri Jul 24, 2009 11:48 am
by gnozal
Michael Vogel wrote:... I want to split long constant lists into more smaller areas for structuring, what about a command which continues enumeration beginning with the highest enumerated constant so far?
It's possible since PB3.9x or am I missing something ?
Code: Select all
Enumeration
#GadgetInfo ; Will be 0
#GadgetText ; Will be 1
#GadgetOK ; Will be 2
EndEnumeration
Enumeration #PB_Compiler_EnumerationValue
#GadgetCancel ; Will be 3
#GadgetImage ; Will be 4
#GadgetSound ; Will be 5
EndEnumeration
Posted: Fri Jul 24, 2009 12:00 pm
by Michael Vogel
gnozal wrote:It's already possible since PB3.9x or am I missing something ?
Code: Select all
Enumeration
#GadgetInfo ; Will be 0
#GadgetText ; Will be 1
#GadgetOK ; Will be 2
EndEnumeration
Enumeration #PB_Compiler_EnumerationValue
#GadgetCancel ; Will be 3
#GadgetImage ; Will be 4
#GadgetSound ; Will be 5
EndEnumeration
You are right, even I wanted to get the highest value ever assigned to an enumerated constant so far - but when using a clear order of the enumeration blocks, your example will show exactly what I have asked for :roll:
So maybe the following example just shows my untypical way of coding - it should demonstrate it better now, what I have ment before
Code: Select all
; Option Window
Enumeration
#OptPanelDisplay; 0
#OptPanelInfos; 1
#OptPanelWaypoints; 2
#OptPanelAltitude; 3
#OptPanelExtras; 4
:
#OptCancel; 87
EndEnumeration
; Option Colors
Enumeration
#OptBlack; 0
#OptWhite; 1
#OptGreen; 2
:
EndEnumeration
; Report Window
Enumeration Continue
#RepSkipDots; 88
#RepSkipInfo; 89
#RepLineColor; 90
#RepInfoType; 91
:
#ReportCancel; 117
EndEnumeration
; Report Values
Enumeration
#RepYearly; 0
#RepMonthly; 1
#RepWeekly; 2
:
EndEnumeration
; Statistic Window
Enumeration Continue
#StaGraph; 118
#StaTable; 119
:
EndEnumeration
Posted: Fri Jul 24, 2009 12:12 pm
by gnozal
Ok, I understand now.
You could do something like this :
Code: Select all
; Windows
Enumeration
#Win0
#Win1
#Win2
#Win3
#Win4
EndEnumeration
#CurrentWindowEnum1 = #PB_Compiler_EnumerationValue
; Gadgets
Enumeration
#Gad0
#Gad1
EndEnumeration
#CurrentGadgetEnum1 = #PB_Compiler_EnumerationValue
; Windows
Enumeration #CurrentWindowEnum1
#Win5
#Win6
#Win7
EndEnumeration
#CurrentWindowEnum2 = #PB_Compiler_EnumerationValue
; Gadgets
Enumeration #CurrentGadgetEnum1
#Gad2
#Gad3
EndEnumeration
#CurrentGadgetEnum2 = #PB_Compiler_EnumerationValue
; Windows
Enumeration #CurrentWindowEnum2
#Win8
#Win9
#Win10
EndEnumeration
#CurrentWindowEnum3 = #PB_Compiler_EnumerationValue
Debug #Gad3
Debug #Win5
Debug #Win10
It would be easier if we could reassign #PB_Compiler_EnumerationValue to the same constant, i.e.
#CurrentGadgetEnum = #PB_Compiler_EnumerationValue several times instead of using #CurrentGadgetEnum1, #CurrentGadgetEnum2, etc...
Posted: Fri Jul 24, 2009 12:16 pm
by freak
Posted: Fri Jul 24, 2009 12:27 pm
by Michael Vogel
This is a really great solution!
Thanks,
Michael
PS these macros should be integrated into the system (to get rid of rewriting the name also in endenumeration line), or it should be done into an include file... -- oi, next idea: maybe a certain file (PureBasic.pbi) should be included by default

Enumeration Final Maybe
Posted: Wed Aug 12, 2009 2:36 pm
by Randy
Here is something that I've often wished for:
Code: Select all
Enumeration 1
#COLOR_RED
#COLOR_YELLOW
#COLOR_GREEN
#COLOR_BLUE
#COLOR_BROWN
#COLOR_BLACK
#COLOR_WHITE
#COLOR_ORANGE
#COLOR_PURPLE
EndEnumeration #TOTAL_COLORS
Global Dim ColorSet(#TOTAL_COLORS)
I've often had large enumeration lists and the only way that I've been able to make this work would be to do something like Global Dim ColorSet(#COLOR_PURPLE) because that would be the last one. But it gets messy: For x = 1 to #COLOR_PURPLE... Yes, I could use ArraySize(...) but having a constant is faster and easier to understand in many cases.
Randy
Posted: Wed Aug 12, 2009 2:40 pm
by Arctic Fox
#PB_Compiler_EnumerationValue

Posted: Wed Aug 12, 2009 4:27 pm
by gnasen
I think it has been wished more then one time but an enumeration like this would be fine:
Code: Select all
#const01 = %00001
#const02 = %00010
#const03 = %00100
#const04 = %01000
#const05 = %10000
maybe like this:
Posted: Wed Aug 12, 2009 5:44 pm
by Demivec
Here's another macro solution for enumerating flags:
Code: Select all
Macro debugBin(var)
Debug "%" + RSet(Bin(var), 8, "0")
EndMacro
Macro nextFlag
(#PB_Compiler_EnumerationValue - 1) * 2
EndMacro
;flags
Enumeration
#bigFlag = 1
#greenFlag = nextFlag
#newFlag = nextFlag
#fastFlag = nextFlag
#strongFlag = nextFlag
#workingFlag = nextFlag
EndEnumeration
debugBin(#bigFlag)
debugBin(#greenFlag)
debugBin(#newFlag)
debugBin(#fastFlag)
debugBin(#strongFlag)
debugBin(#workingFlag)
Posted: Thu Aug 13, 2009 7:51 am
by horst
gnozal wrote:It would be easier if we could reassign #PB_Compiler_EnumerationValue to the same constant, i.e. #CurrentGadgetEnum = #PB_Compiler_EnumerationValue several times instead of using #CurrentGadgetEnum1, #CurrentGadgetEnum2, etc...
A constant that can be changed is not a constant.
It is time to think about the concept of
compiler variables (in contrast to runtime variables). For example, #PB_Compiler_EnumerationValue
looks like a constant, but it is not a constant. It is a compiler variable. Only at the point of compilation it is used like a constant.
I think PureBasic should introduce the concept of compiler variables (instead of offering "not really constant constants")...
Posted: Fri Aug 14, 2009 1:39 pm
by Randy
Arctic Fox wrote:#PB_Compiler_EnumerationValue

True, that would work. I had forgotten about that little internal value. I could then, hypothetically, use #TotalCount = #PB_Compiler_EnumerationValue. I'll give that a shot.
[Edit] It actually returns the next one so if the last value is 4, #PB_Compiler_EnumerationValue will actually equal 5.
Still, I could deduct one from it during assignment. At least it it frees me from having to make sure that a value is always at the bottom of the list.
[End of Edit]
Thanks for that tip,
Randy
Re: Enumeration enhancements
Posted: Fri Feb 22, 2013 6:06 pm
by Michael Vogel
I'm sure, somewhere in the forum is a fine solution for doing binary enumerations, but I couldn't find it...
Code: Select all
Macro BinaryEnumeration
((#PB_Compiler_EnumerationValue-(#PB_Compiler_EnumerationValue>1))<<(#PB_Compiler_EnumerationValue>1))
EndMacro
Enumeration
#FlagA =BinaryEnumeration
#FlagB =BinaryEnumeration
#FlagC =BinaryEnumeration
#FlagD =BinaryEnumeration
#FlagE =BinaryEnumeration
EndEnumeration
Debug #FlagA
Debug #FlagB
Debug #FlagC
Debug #FlagD
Debug #FlagE
...that's why I am using the code above for a while. Is there a simple way to update it for PB5.10?
Re: Enumeration enhancements
Posted: Fri Feb 22, 2013 6:18 pm
by Demivec
Michael Vogel wrote:I'm sure, somewhere in the forum is a fine solution for doing binary enumerations, but I couldn't find it...
My solution is three posts up.
Michael Vogel wrote:...that's why I am using the code above for a while. Is there a simple way to update it for PB5.10?
What's wrong with the code?
Re: Enumeration enhancements
Posted: Fri Feb 22, 2013 8:21 pm
by Michael Vogel
Demivec wrote:My solution is three posts up.
Demivec wrote:What's wrong with the code?
Nothing up to PB 5.0, and I can use the macro for the first constant as well. But when using 5.1, I'll get an error, that boolean expressions are not allowed without the new Bool function, but Bool's results are non-integer values and bring up another error message...
I don't like the following code, but it works at least:
Code: Select all
Macro BinaryEnumeration
((#PB_Compiler_EnumerationValue-1)<<1+((#PB_Compiler_EnumerationValue-2)&$8000000+(~-#PB_Compiler_EnumerationValue)&$8000000)>>27)
EndMacro