4
votes

Within an IronPython script, I'm trying to compare the property (of type Grade) of an object (created in C#) with one of the possible values of the Grade enum. So for example:

if (myObject.TotalGrade == Grade.Fail):

Now, I have set a breakpoint after obtaining both of these values, and I can indeed verify that both of them hold a 'Fail'; however, the check fails and the program moves on to the next clause. Is the python equality operator not suitable for C# enums?

Additional info: I have imported the Grade enum from my C# as shown below.

import clr
clr.AddReferenceToFile("mydll.dll")
from mydll import Grade
1
Not sure why they wouldn't compare equal, but apparently enums are implemented as boxed types in IronPython -- it seems you have two different objects for the same enum value. You can work around this by comparing the underlying values directly via myObject.TotalGrade.value__ == Grade.Fail.value__. - Cameron
@Cameron Wow, I don't know how you found that, but man is that exchange of emails priceless. Thanks for solving this mystery. Can you post your comment as an answer so I can accept it? - estebro
Added an answer. As for the mailing list, I just Googled around a bit regarding IronPython enums ;-) - Cameron
Thanks again for the help. - estebro

1 Answers

4
votes

As requested, my comment as an answer:

I'm not sure why they wouldn't compare equal, but apparently enums are implemented as boxed types in IronPython -- it seems you have two different objects for the same enum value in this case.

You can work around this by comparing the underlying values directly like so:

if myObject.TotalGrade.value__ == Grade.Fail.value__:
    pass  # your code here...