http://www.aygfsteel.com/ebecket 返還網(wǎng)
          隨筆-140  評論-11  文章-131  trackbacks-0

          I have an enum SortFilter, which looks like following:

          publicenum SortFilter

          {

                FirstName,

                LastName,

                Age,

                Experience

          }

          Now, let's say I want to display the string value of enum in some control. For that, I will have to convert Enum value to string. For example, I want to add all enum string values to a DropDownList. The following code loops through the enumeration and adds string values to it. Here SortByList is DropDownList.

          SortByList.Items.Clear();

          // Conversion from Enum to String

          foreach (string item in Enum.GetNames(typeof(ArrayListBinding.SortFilter)))

          {

                SortByList.Items.Add(item);
          }    

          This code converts an enum to string:

          string name= Enum.GetName(typeof(ArrayListBinding.SortFilter), SortFilter.FirstName);

          Now let's say, you have an enum string value say, "FirstName" and now you want to convert it to Enum value. The following code converts from a string to enum value, where Developer.SortingBy is of type SortFilter enumeration:

          // Conversion from String to Enum

          Developer.SortingBy = (SortFilter)Enum.Parse(typeof(SortFilter), "FirstName");  

          posted on 2009-11-20 17:53 becket_zheng 閱讀(379) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 齐齐哈尔市| 鄱阳县| 宝丰县| 常德市| 新兴县| 东莞市| 什邡市| 隆回县| 隆德县| 阜南县| 永仁县| 合水县| 农安县| 海伦市| 津市市| 饶平县| 阳江市| 修文县| 榕江县| 丰都县| 博罗县| 北碚区| 武宁县| 兴化市| 汤原县| 灵武市| 南澳县| 屏南县| 大同市| 旬邑县| 孝昌县| 红桥区| 小金县| 霍林郭勒市| 江油市| 青铜峡市| 浦东新区| 东安县| 钦州市| 宜兰市| 镇江市|