Candy

:★..CANDY LOVE~。。●じ
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

How to Use Enum?

Posted on 2008-05-22 14:25  丽红  阅读(197)  评论(0编辑  收藏  举报

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