Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Processing Enums
Message
 
 
To
17/09/2014 15:45:47
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01607747
Message ID:
01607751
Views:
31
>>Hi everybody,
>>
>>We have the following code to process Enums and convert them into a list of values to select from
>>
>>
>>T[] enumValues = (T[])Enum.GetValues(typeof(T));
>>            string[] enumNames = (string[])Enum.GetNames(typeof(T));
>>
>>            for (int i = 0; i < enumValues.Length; i++)
>>            {
>>                bool selected = false;
>>                object enumUnderlyingValue = Convert.ChangeType(enumValues[i], enumUnderlyingType);
>>
>>                if (omitEnumValue != null && enumUnderlyingValue.Equals(omitUnderlyingValue))
>>                {
>>                    // The Enum value matches the omitValue parameter, so we won't process it.
>>                    // This check is for enums that have a default value such as NotSpecified
>>                    // which we may not always want to include in a dropdown.
>>                }
>>                else
>>                {
>>                    Int32 enumValue = Convert.ToInt32(enumUnderlyingValue);
>>                    string enumName = GetFormattedEnumName<T>(enumNames[i]);
>>
>>                    if (selectedUnderlyingValue != null)
>>                    {
>>                        selected = enumUnderlyingValue.Equals(selectedUnderlyingValue) ? true : false;
>>                    }
>>
>>                    items.Add(GetNewSelectViewModel(selected, enumName, enumValue));
>>                }
>>            }
>>
>>There is, unfortunately, a bug here. This code assumes that each enum simply has values corresponding to their number in the list, which is not true. Say, I have the following enum
>>
>>
>>  public enum LookupTypes : byte
>>    {
>>        [Description("Select Lookup Method")]
>>        Typing = 1,
>>        HotKeySelect = 2,
>>        SingleKeyLookup = 3,
>>        MultiKeyLookup = 4
>>    }
>>}
>>
>>and I noticed that my drop-down didn't have the value I specified as selected using this code
>>
>>
>> var lookupTypes = EnumUtilities.EnumToEnumViewModel<LookupTypes>(selectEnumValue: LookupTypes.Typing);
>>
>>so I started to debug this code and thus how I found this bug.
>>
>>The problem is - how to now resolve it to use the actual value of enum instead of its number?
>>
>>Thanks in advance.
>
>If you don't post your complete function, its hard to tell where the bug is. There is nothing here that indicates where or how selectedUnderlyingValue is populated. Given that you are converting enumUnderlyingValue to what I assume is a boxed byte, and there is another conversion of values to int, I would speculate that selectedUnderlyingValue is being set as an int somewhere and its the comparison between a byte and an int that is failing.

Hi Rob,

Sorry, I posted the problem a lit bit too quickly. I called my colleague and together we figured this out. It was actually my mistake and the function worked just fine. The problem lied in this function
string enumName = GetFormattedEnumName<T>(enumNames[i]);
This function, it turn, did the following
 private static string GetFormattedEnumName<T>(string enumName)
        {
            // If the enum value has a Description attribute then use
            // the Description property of that attribute for the
            // enum name.  Otherwise, split the enum name on its
            // camel casing and use that for the name.

            string formattedEnumName = GetEnumDescription<T>(enumName);

            if (formattedEnumName == null)
            {
                formattedEnumName = SplitCamelCase(enumName);
            }

            return formattedEnumName;
        }
BTW, do you see a reason of having this function static?

GetEnumDescription was another very complex function that grabbed the description of the enum if there was one. Now, as you see in the Enum definition (which I copied from another enum when I created it), it had a description for the first element Typing. For some reason I didn't realize it was description of that particular value and not the whole enum. So, the behavior I found was not a bug but rather a feature. To correct it all I needed to do was to remove this description.

Thanks again.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform