如何对我的枚举属性实施set方法? [英] How to implement set method to my enum property?

查看:270
本文介绍了如何对我的枚举属性实施set方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
    }
}



我该如何实施?

仅供参考:
1)我必须根据枚举结果填充组合框.
2)此代码将在UserControl中实现.

提前致谢..! :)



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..! :)

推荐答案

此方法将从MY_L​​ist属性获取当前设置的名称.
This method will get the currently set name from the MY_List property.
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();
}


解决方案2很好,我对此投了5票.
但是,如果您想为枚举成员提供更多描述性的名称,比如说要包含标识符不允许使用的空格,那么下面的文章可能会对您有所帮助.

​​枚举类型不枚举!解决.NET和语言限制 [ ^ ]
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[^]


ProEnggSoft已经推荐了我的文章,但并不完全正确.正确的是下一个,基于循环中的第一个.这是真正真正的问题解决方案,它包括本地化并且可以本地化,此外,它还可以以人类可读的形式显示值(例如,选项3"而不是选项3").请参阅:
人类可读的枚举元数据 [ ^ ].

—SA
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


这篇关于如何对我的枚举属性实施set方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆