Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Enum bug with multiple value

Discussion in 'Scripting' started by CrymX, Feb 8, 2017.

  1. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Hello guys, can you confirm this is impossible to do with unity ?

    classic compiler :


    Code (CSharp):
    1.  public enum test
    2.     {
    3.         a,
    4.         b,
    5.         c,
    6.         d= b&e&f,
    7.         e,
    8.         f,
    9.     }
    10.     public class Program
    11.     {
    12.         public static void Main(string[] args)
    13.         {
    14.             //Your code goes here
    15.             var b = test.b;
    16.             var bef= test.d;
    17.             var e = test.e;
    18.            var a =test.a;
    19.        
    20.        
    21.             if(b == bef)
    22.             {
    23.                 Console.WriteLine("b = bef");
    24.             }
    25.        
    26.             if(Enum.Equals( bef==b))
    27.             {
    28.                 Console.WriteLine("bef= b");
    29.             }
    30.        
    31.             if(a ==bef)
    32.             {
    33.                 Console.WriteLine("a = bef");
    34.             }
    35.            
    36.         }
    37.     }
    RESULT normal compiler :
    b = bef
    bef = b
    RESULT Unity :
    a =bef

    Anyone has seen this bug ?
     
  2. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    on the debug view :

    bugEnum.png
     
  3. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    What is this 'normal compiler' that gives the result you refer to?

    I may be wrong, as I'm not up to date with the latest c# behaviour, but the '=' when defining an enum is giving its integer value, so you can compare the enum value using an integer. Whilst the & is a logical "and" (does a bitwise "and"), which would give bef a binary value of 001&100&101 which is 0, which is the same as a. So, from what I can see Unity is giving the answer I'd expect.

    So, to expand, I think the enum values would have the integer equivalents of:
    a =0 (binary 000),
    b =1 (binary 001),
    c =2 (binary 010),
    d = 001&100&101=0 (binary 000)
    e = 4 (or 3?, but will assume 4) (binary 100)
    f = 5 (or 4) (binary 101)

    Even if e is 3 (011) and f is 4 (100) d would be 0 (000)
     
    Last edited: Feb 8, 2017
  4. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    Actually, I thought about it a bit more, and couldn't see how a compiler would cope, so gave it a quick try, and the compiler complained about a circular definition . So it wouldn't compile (which makes sense).

    So I'm not sure what's going on with your code (how any result is returned).

    However I've never come across an enumeration with one enum value equivalent to multiple others.
     
    Last edited: Feb 8, 2017
  5. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    i have tested with .NET 4.5
     
  6. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    You are right, my test was F***ed up
     
  7. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    I have found another solution with the flagAttribute, work as i want to


    Code (CSharp):
    1.  [FlagsAttribute]
    2.     public enum TypeResource
    3.     {
    4.        
    5.         a = 1,
    6.         b= 2,
    7.         c = 4,
    8.         d = 8,
    9.         e = b|c|d,
    10.             f= 16
    11.     }
    12.     class Program
    13.     {
    14.         static void Main(string[] args)
    15.         {
    16.            
    17.             var bcd = TypeResource.e;
    18.             var a = TypeResource.a;
    19.             var b = TypeResource.b;
    20.             var c = TypeResource.c;
    21.             var f = TypeResource.f;
    22.             Console.WriteLine(a == bcd);
    23.             Console.WriteLine(b == bcd);
    24.             //false
    25.             //false
    26.             Console.WriteLine(bcd.HasFlag(a));
    27.             Console.WriteLine(bcd.HasFlag(b));
    28.             Console.WriteLine(bcd.HasFlag(c));
    29.             Console.WriteLine(bcd.HasFlag(f));
    30.  
    31.             //false
    32.             //true
    33.             //true
    34.             //false
    35.  
    36.             Console.Read();
    37.         }
    38.     }