Asked By
Jamin Ywain
60 points
N/A
Posted on - 09/18/2012
Hi,
I have 2 items compared. Unfortunately,they don’t allow comparing these items because one is a string value the other is an Enum.
How can I convert a string value to Enum so that I could compare this two?
Can I have a simple code on converting string to Enum?
Microsoft Dynamics AX Debugger
Error executing code Wrong argument types for comparison. Stack trace
(C)FormsSysFillUtilityMethodsinitializeMainQuery – line 69
(C)FormsSysFillUtilityMethodsinit – line 53
(C)ClassesSysSetupFormRuninit – line 3
(C)ClassesxMenuFunctionrun
(C)ClassesMenuFunctionrun – line 87
(C)FormsSysRecordlnfoDesignsDesignListFillButtonMethodsClick
ed – line11
Â
Need help with Enums in C sharp
This error shows when you compare enum types and they don't match. It can also happen for the Boxing in C#.
If you want to avoid boxing , you should compare generics. You can do it with EqualityComparer<T>.Default
. This does IEquatable<T>
(without boxing) and also object.Equals
, and locates all the Nullable<T>
"lifted" nuances. Hence there shouldn't be any error regarding enumeration.
Need help with Enums in C sharp
Â
Simplest solution is to cast your Enum to string. Then you can compare string values. Try the following code
publicenum EItems
{
   item1=11,
   item2=2,
   item3=3
Â
     Â
}
Â
      stringa = "1";
       int b = (int)EItems.item1;
       if (b.ToString() == a)
       {
Â
       }