zoukankan      html  css  js  c++  java
  • How to Use Enum?

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;


    public partial class _Default : System.Web.UI.Page
    {
        /// <summary>
        /// Practicing Enum technic
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //name => value
            int a = (int)Alignment.aa.Center;
            Response.Write("name => value: " + a.ToString());

            //value =>name
            string a1 = ((Alignment.aa)Enum.Parse(typeof(Alignment.aa), "1", false)).ToString();

            Response.Write("</BR>value =>name: " + a1);

            //Search all items' name in Enum
            int i = 0;
            string arrStr = "";
            foreach(string s in Enum.GetNames(typeof(Alignment.aa)))
            {
                arrStr += "arr[" + i.ToString() + "]= " + s + " ";
                i++;
            }

            Response.Write("</BR>Names:" + arrStr);

            //Search all items' value in Enum
            int j = 0;
            string arrStrValue = "";
            foreach (int v in Enum.GetValues(typeof(Alignment.aa)))
            {
                arrStrValue += "arr[" + j.ToString() + "]= " + v.ToString() + " ";
                j++;
            }

            Response.Write("</BR>Values:" + arrStrValue);


            //Search the names and values of all items in Enum
            if (Enum.GetValues(typeof(Alignment.aa)).Length != Enum.GetNames(typeof(Alignment.aa)).Length) return;

            int k = 0;
           
            string arrStrs ="";
            foreach (string s in Enum.GetNames(typeof(Alignment.aa)))
            {
                int AliValue = 0;
                if (s == "Center")
                {
                    AliValue = (int)Alignment.aa.Center;
                }
                else if (s == "Left")
                {
                    AliValue = (int)Alignment.aa.Left;
                }
                else if (s == "Right")
                {
                    AliValue = (int)Alignment.aa.Right;
                }
               
               arrStrs += "arr[" + k.ToString() + "]: " + s + "-" + AliValue.ToString() + " ";
                k++;
            }

            Response.Write("</BR>" + "Name-Value:" + arrStrs);
        }
    }



    Class:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    /// <summary>
    /// Summary description for Alignment
    /// </summary>
    public class Alignment
    {
     public Alignment()
     {
      //
      // TODO: Add constructor logic here
      //
     }
        public enum aa
        {
            Left,
      Center,
      Right
        }
    }

  • 相关阅读:
    021.day21 反射 Class类 反射常用操作
    020.day20 线程概述 多线程优缺点 线程的创建 线程常用方法 生命周期 多线程同步
    019.day19 缓冲流 对象流 标准输入输出流
    018.day18 map集合如何实现排序 File类 IO流 字节流 字符流 编码
    017.day17 Map接口 克隆 treeSet集合排重缺陷
    016.day16 HashSet TreeSet 比较器Comparable Comparator
    015.day15
    014.day14
    013.day13
    线程
  • 原文地址:https://www.cnblogs.com/Candy/p/1204834.html
Copyright © 2011-2022 走看看