Need help with Enums in C sharp

Asked By 60 points N/A Posted on -
qa-featured

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

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

 

SHARE
Best Answer by Niktha Busch
Best Answer
Best Answer
Answered By 0 points N/A #145864

Need help with Enums in C sharp

qa-featured

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.

Answered By 5 points N/A #145865

Need help with Enums in C sharp

qa-featured

 

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)

        {

 

        }

Related Questions