none
Enums can not have duplicate values RRS feed

  • Question

  • Hi,

    I have a constant.cs file which have different type of enums defined. My requirement is that whenever a duplicate value is defined to enum and build the solution, the compiler will throw an aception and can't build the solution. for example:

    enum departments
    {
    IT = 1000,
    HR = 1001,
    Accounts = 1000, //this have a duplicate value as IT
    }

    then the compiler will throw an exception like "enum 'department' have duplicate enum value define" during building/rebuilding the solution.

    Saturday, August 30, 2008 6:32 PM

Answers

  • Hello All,

    Finally I have resolved my problem. Now I let you know how this problem got resolved. I have create a macro project and tracked the project where I have  created the class containing duplicate enums. Implemented the logic there to find the duplicate enum values and canceled the build when I found the duplicate enum value. Guys you can directly cancel the build using "build.cancel" commend.

    For detailed logic, feel free to contact me at dobhal.bipul@gmail.com and I will post the solution in my blog "dobhalshaahb.blogspot.com" so you may refer this blog for the same.

    Cheers!!!
    Bipul Dobhal
    • Marked as answer by BIPUL DOBHAL Wednesday, October 15, 2008 6:35 PM
    Wednesday, October 15, 2008 6:35 PM

All replies

  • This code is valid per CLR and C# rules, so there is no compile-time error.

    If you really need to check this, you could call Enum.GetValues on this enum type and then see if any such value when passed to Enum.GetNames returns an array with more than one element.  You could run this at the beginning when your program starts up and exit the program if you find a problem.



    • Marked as answer by nobugzModerator Sunday, August 31, 2008 9:54 PM
    • Unmarked as answer by BIPUL DOBHAL Monday, September 1, 2008 5:22 PM
    • Unmarked as answer by BIPUL DOBHAL Monday, September 1, 2008 5:22 PM
    • Unmarked as answer by BIPUL DOBHAL Monday, September 1, 2008 5:22 PM
    • Unmarked as answer by BIPUL DOBHAL Monday, September 1, 2008 5:22 PM
    Saturday, August 30, 2008 6:52 PM
  • Thanks for your reply.

    But as per the solution suggested by you, I can throw the exception at run time. But my requirement here is to throw the exception at compile time.

    Is there any command/way to call a function of a class in "pre-build event"?

    I will write the whole logic of checking the enum values and throwing the exception in a function and call that function in the pre-build event. I think pre-build events gets executed before compiling the solution.
    Monday, September 1, 2008 3:18 PM
  • Surely, you'd want to use the post-build event so you can check the generated assembly with Reflection.
    Hans Passant.
    Monday, September 1, 2008 6:00 PM
    Moderator
  • Or create a custom FxCop rule?

    Mattias, C# MVP
    Tuesday, September 2, 2008 5:56 AM
    Moderator
  • Hi,

    Can you give me the exact coding guideline or URL to implement this kind of solution?

    Thanks,
    Bipul Dobhal
    Tuesday, September 2, 2008 12:44 PM
  • Also note that I am using .net 2.0 with visual studio 2005. I think custom FxCop kind of rules can be created in .net 3.0 or above in visual studion 2008.

    So I would like to implement with reflection using post-build event. But can any body help me how to implement this.
    Tuesday, September 2, 2008 12:50 PM
  • Hello All,

    Finally I have resolved my problem. Now I let you know how this problem got resolved. I have create a macro project and tracked the project where I have  created the class containing duplicate enums. Implemented the logic there to find the duplicate enum values and canceled the build when I found the duplicate enum value. Guys you can directly cancel the build using "build.cancel" commend.

    For detailed logic, feel free to contact me at dobhal.bipul@gmail.com and I will post the solution in my blog "dobhalshaahb.blogspot.com" so you may refer this blog for the same.

    Cheers!!!
    Bipul Dobhal
    • Marked as answer by BIPUL DOBHAL Wednesday, October 15, 2008 6:35 PM
    Wednesday, October 15, 2008 6:35 PM