About Monkey 2 › Forums › Monkey 2 Programming Help › Enums basics (EDIT: and Bug?)
Tagged: Enum
This topic contains 2 replies, has 2 voices, and was last updated by
Mark Sibly
1 year, 10 months ago.
-
AuthorPosts
-
May 27, 2017 at 10:30 am #8293
two questions (EDIT:+ bug found on 2nd post?):
-are we supposed to declare new enums like I did in the following code ‘e:myEnum’ and assign them their default member? (the code works fine but may I do that?)
-is there a way to get binary shifts members with some kind of flag while declaring an enum or should we do it as I did in the code below?
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940[crayon-5cba166e8914c341873063 inline="true" ]Namespace myapp#Import "<std>"Using std..Enum myEnuma,b,c,d,eEndEnum binEnuma=1b=1 Shl 1c=1 Shl 2d=1 Shl 3EndFunction Main()Local i:IntLocal j:IntLocal e:myEnumi=ePrint i 'prints 0e=myEnum.ei=ePrint i 'prints 4i=e.bPrint i 'prints 1e=e.d|e.ei=ePrint i 'prints 7j=binEnum.b|binEnum.ePrint jEndMay 27, 2017 at 11:17 am #8294I have some unexpected behaviuor here! Am I doing something wrong or should i post an issue?
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233[crayon-5cba166e8d384522471786 inline="true" ]Namespace myapp#Import "<std>"Using std..Enum myEnuma=1b=10c=100d=1000e=10000EndFunction Main()Local i:IntLocal e:myEnume=myEnum.c|myEnum.e|myEnum.ai=ePrint i 'should print "10101"? and prints it!e=e.b|e.di=ePrint i 'should print "1010"? and prints "1002"If e=myEnum.b|myEnum.d Then Print "equals ok"e=myEnum.c|myEnum.e|myEnum.di=ePrint i 'should print "11100"? prints "10236"If e=e.c|e.d|e.e Then Print "equals ok"End[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536[crayon-5cba166e8d38a522068654 inline="true" ]#Import "<std>"Using std..Enum myEnuma,b,c,d,eEndFunction Main()Local i:IntLocal j:intLocal E:myEnumLocal F:myEnumE=myEnum.b|myEnum.d|myEnum.ei=EPrint i 'should print 8? prints 7F=E.d|E.ej=FPrint i 'should print 7? prints 7If F=E 'F(d,e), E (b,d,e) so I suppose E<>F but it detects it as equalPrint "problem 1"ElsePrint "no problem 1"EndIf myEnum.b|myEnum.d|myEnum.e=myEnum.d|myEnum.e 'here it detects it as equal (very strange?)Print "even bigger problem? 2"ElsePrint "no big problem? 2"EndIf myEnum.b|myEnum.d|myEnum.e=E.e|E.b ' here it detects it as not equal (nice)Print "even bigger problem? 3"ElsePrint "no big problem? 3"EndEndMay 28, 2017 at 12:38 am #8322Yes, that’s how enum’s work. There’s no mechanism for auto-generating ‘mask’ enum values, eg: 1 Shl 1, 1 Shl 2, 1 Shl 3 etc. I find it useful to use hex for these:
Monkey123456789101112Enum FlagsA=$0001B=$0002C=$0004D=$0008E=$0010F=$0020G=$0040H=$0080EndLocal flags:=Flags.A | Flags.BAs for the ‘bugs’, I think you might be getting operators ‘|’ and ‘+’ mixed up! X|Y is NOT the same as X+Y. The code is working, but your enum values have multiple bits set so you’re getting weird values when you ‘|’ them together.
-
AuthorPosts
You must be logged in to reply to this topic.