Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Referring to enums
Message
From
31/01/2011 08:42:28
 
 
To
31/01/2011 08:32:47
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01497627
Message ID:
01497949
Views:
24
>>>It is stored as an int in the table.
>>>
>>>I use MM.NET and this binds the combo to a BO so I don't actually manually set the datasource.
>>>
>>>I don't quite follow your suggestion (I'm still a baby when it comes to this .NET C# stuff and the terminology), but if you don't have the time to explain further, no problem as Bonnie's suggestion has worked for me.
>>
>>OK. I'm confused as to how the ComboBox.SelectedValue ends up as a string in that scenario - but as long as its working.......
>>( I assume you tested this with non-matching descriptions ?)
>>
>
>That makes two of us!

As long as it works if, for example, you use a description such as "Death Notice" instead of "DeathNotice"............



>>
>>>>So how are you storing the enum value in the table - as a string or an int ?
>>>>If an int and you use this as the SelectedValue of the ComboBox then the example code below works and is more efficient than a string.
>>>>OTOH why not build (say) a generic list from the table and cast the int (or string) to an actual enum value during intialiazation so that your subsequent code, whereever used, need have no need to repeat the operation.....
>>>>
>>>>
>>>>>the datasource of the combo is a lookup table which has the code and the description. The enum is just a way that was supposed to make it easy for me during coding (the user can change the descriptions, but not the codes in the table).
>>>>>
>>>>>
>>>>>>What is the DataSource for the ComboBox. If you use the enum for this like:
comboBox1.DataSource = Enum.GetValues(typeof (StatusCode));
then you can just use:
if ((StatusCode)comboBox1.SelectedValue == StatusCode.DeathNotice)
>>>>>>            {
>>>>>>            }
But this gets a bit more complicated if you don't want to use the exact enum string value in the ComboBox :-{
>>>>>>
>>>>>>>I think I made a wrong assumption using these enums.
>>>>>>>
>>>>>>>I now want to compare the selectedvalue of a drop down list with the value of the enum. How do I do that?
>>>>>>>
>>>>>>>My code used to be like this:
>>>>>>>
>>>>>>>
if (this.cboStatus.SelectedValue == "7")
>>>>>>>
>>>>>>>and that worked, but I don't like magic numbers so wanted to code it like this:
>>>>>>>
>>>>>>>
if (this.cboStatus.SelectedValue == Policy.StatusCode.DeathNotice.ToString())
>>>>>>>
>>>>>>>thinking that Policy.StatusCode.DeathNotice.ToString() would bring back "7", but it doesn't, it returns "DeathNotice"!
>>>>>>>
>>>>>>>>>On another note, just an FYI: it's not necessary to provide a number for your Enums (Pending = 1), they will default to numbers in the order you've written, starting at 0. If you want yours to start at 1, as you've shown below, or if you need a different numbering scheme for some reason, then you would have to explicitly define them as you did.
>>>>>>>>
>>>>>>>>Strictly speaking it would only be neccessary to define the value for the first item, the rest will still increment. You can also skip as below ( and save 24bits by using a byte):
enum StatusCode : byte
>>>>>>>>    {
>>>>>>>>        Pending = 1,
>>>>>>>>        InForce,
>>>>>>>>        Withdrawn,
>>>>>>>>        Postponed,
>>>>>>>>        Deferred,
>>>>>>>>        Cancelled =100,
>>>>>>>>        Claimed,
>>>>>>>>        Matured,
>>>>>>>>        Lapsed, 
>>>>>>>>        Surrendered,
>>>>>>>>        DeathNotice
>>>>>>>>    }
>>>>>>>>
>>>>>>>>
>>>>>>>>>>Hi,
>>>>>>>>>>
>>>>>>>>>>I have an enum defined in a partial class of a Business Object (all my BOs are in one project) like this:
>>>>>>>>>>
>>>>>>>>>>
        enum StatusCode
>>>>>>>>>>        {
>>>>>>>>>>            Pending = 1,
>>>>>>>>>>            InForce = 2,
>>>>>>>>>>            Withdrawn = 3,
>>>>>>>>>>            Postponed = 4,
>>>>>>>>>>            Deferred = 5,
>>>>>>>>>>            Cancelled = 6,
>>>>>>>>>>            Claimed = 7,
>>>>>>>>>>            Matured = 8,
>>>>>>>>>>            Lapsed = 9,
>>>>>>>>>>            Surrendered = 10,
>>>>>>>>>>            DeathNotice = 11
>>>>>>>>>>        }
>>>>>>>>>>
>>>>>>>>>>This lets me refer to the enum in code in the partial class no problem, like this:
>>>>>>>>>>
>>>>>>>>>>
this.Entity.Status = StatusCode.Pending;
>>>>>>>>>>
>>>>>>>>>>However, if I want to use this enum in the interface project (separate to the BO project, but part of the overall solution) I can't as the compiler does not recognise it. Is there some way to make this enum available outside of the BO? Or is this bad design?
Previous
Reply
Map
View

Click here to load this message in the networking platform