Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
public enum ListType
{
    enum1,
    enum2,
    enum3,
    enum4,
    enum5
}

public ListType My_List
{
    get { return GetList(); }
    set { SetList(); }
}

private void SetList()
{
    switch(My_List)
    {
        case ListType.enum3
                // i want to get some name of the property set in enum My_List i.e.
                // i.e. if the property is set to enum3 i want enum3
            break;
    }
}


How can I implement this?

FYI:
1) I have to fill the Combobox based on enum result.
2) This code will be implemented in UserControl.

Thanks in advance..! :)
Posted
Updated 19-Mar-12 19:09pm
v2

This method will get the currently set name from the MY_List property.
C#
public enum ListType
{
    enum1,
    enum2,
    enum3,
    enum4,
    enum5
}

private ListType _my_List;
public ListType My_List
{
    get { return _my_List; }
    set { _my_List = value; }
}

private string GetListName()
{
    return Enum.GetValues(typeof(ListType)).Cast<ListType>().FirstOrDefault(x => x.ToString() == _my_List.ToString()).ToString();
}
 
Share this answer
 
v3
Comments
AJV King 20-Mar-12 1:52am    
Thanks a TON Dean you made my day...!! :D High 5 STAR..!!

It really works...!
ProEnggSoft 20-Mar-12 1:57am    
Good solution. +5
Solution 2 is good, I voted 5 for it.
But, if you want more descriptive names for the members of enum, say you want to include spaces, which are not permitted in identifiers, then the following article may be helpful to you.

Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 20-Mar-12 2:18am    
Thank you for quoting my article, but this is not exactly the one you should have recommended, it should be the second one from this cycle, based on the one you referenced. Please see my answer; it's more adequate to the question.
--SA
ProEnggSoft 20-Mar-12 2:42am    
Sorry! I made a mistake. First I opened Human-readable Enumeration Meta-data from there I opened the above link. I pasted a wrong link. Thank you for correcting it.
ProEnggSoft already recommended my article, but not exactly the right one. The right one is the next one, based on the first from the cycle. This is the really comprehensive solution of the problem, it includes localization and can be localized, among other thing, besides, it can present values in human readable form ("Option 3" instead of "option3", for example). Please see:
Human-readable Enumeration Meta-data[^].

—SA
 
Share this answer
 
Try this out:

C#
private String SetList()
{
    String listName;
    switch(My_List)
    {
        case ListType.enum3
            listName = "enum3"
            break;
        case ListType.enum2
            listName = "enum2"
            break;
    }
    return listName;
}
 
Share this answer
 
Comments
AJV King 20-Mar-12 1:31am    
But the switch does not accept enum "My_List"...!
Sergey Alexandrovich Kryukov 20-Mar-12 2:14am    
Very, very, very bad. Honestly, how can you give solutions like that? Did you ever think about its support. The real solution is very apparent...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900