Day: February 22, 2018

  • GET STRING FROM ENUM

    เพื่อนๆนักพัฒนาหลายคนก็คงจะรู้จัก enum กันพอสมควรแล้วนะคะ วันนี้เรามาทำความรู้จักเจ้า Enum กันให้มากขึ้นกว่าเดิมกันดีกว่านะคะ ว่านอกจากเราจะดึงค่า Integer ที่เก็บค่าในตัวแปร หรือ Tostring() เป็นค่าstring ตามชื่อของตัวแปร Enum แล้ว เรายังสามารถ ดึงค่า  String  เป็นประโยคยาวๆได้โดยที่ไม่จำเป็นต้องเหมือนกับชื่อตัวแปรแล้วนะคะ เอาล่ะค่ะ เรามาเริ่มจากการทำความรู้จักเจ้าตัว Enum กันตั้งแต่เริ่มต้นเพื่อระลึกความจำกันก่อนละกันนะคะ ^^

    Enumeration จะมีชนิดเป็น integer type ซึ่ง เราสามารถกำหนดกลุ่มของข้อมูลได้ (User-defined)

    ในการสร้าง enumeration เราจะใช้ keyword คำว่า  enum  ดังนี้

    public enum Colors
    {
      White,
      Black,
      Red,
      Green,
      Blue
    }

    ในตัวอย่างนี้สร้าง enum ที่ชื่อว่า Colors โดยมีสมาชิกอยู่ 5 สมาชิกคือ White,Black,Red,Green,Blue

    โดยสมาชิกต่างๆของ enum ถ้าเราไม่ได้กำหนดค่าเริ่มต้นจะมีค่าเริ่มต้นจาก 0 ดังนี้

    white = 0

    Black =1

    Red =2

    Green=3

    Blue=4

    ในการเรียกใช้งาน enum

    Colors  c = Colors.Green; //สร้างตัวแปร c ขึ้นมาเพื่อเรียกใช้สมาชิกที่ชื่อ Green

    ตัวอย่าง enum

     

    using System;
    public enum Colors
    {
        White,
        Black,
        Red,
        Green,
        Blue
    }
    public class TestEnum
    {
        public static void Main()
        {
            Colors c = Colors.Blue;
            Console.WriteLine(c);
            Console.ReadLine();
        }
    }

    ผลลัพธ์จะแสดง Blue

    จากตัวอย่างนี้ถ้าต้องการให้แสดงเป็น integer type   เราก็เขียนได้ดังนี้

    Console.WriteLine((int)c);

    ผลลัพธ์จะได้ 4

    เราสามารถกำหนดค่าให้กับสมาชิกของ  enum ได้ดังนี้

     

    public enum Colors
    {
      Red = 10,
      Green =20,
      Blue=30
    }

    ตัวอย่าง

     

    using System;
    public enum Colors
    {
      Red = 10,
      Green =20,
      Blue=30
    }
    public class TestEnum
    {
        public static void Main()
        {
            Colors c = Colors.Green;
            Console.WriteLine((int)c);
            Console.ReadLine();
        }
    }

    ผลลัพธ์จะได้ 20
    นอกจากนี้สามารถเขียนรับค่า value มาจาก enum ได้อีกแบบคือ
    Colors c = (Colors)Enum.Parse(typeof(Colors), “Blue”, false);
    ตัวอย่าง

    using System;
    public enum Colors
    {
      Red = 10,
      Green =20,
      Blue=30
    }
    public class TestEnum
    {
        public static void Main()
        {
            Colors c = (Colors)Enum.Parse(typeof(Colors), “Blue”, false);
            Console.WriteLine((int)c);
            Console.ReadLine();
        }
    }

    ผลลัพธ์จะได้ 30

    กรณีที่เราจะใช้ tostring เข้ามาใช้ก็สามารถทำได้
    ตัวอย่าง

     

    using System;

    public enum Colors
    {
      Red = 10,
      Green =20,
      Blue=30
    }
    public class TestEnum
    {
        public static void Main()
        {
            string  c = Colors.Green.tostring();
            Console.WriteLine(c);
            Console.ReadLine();
        }
    }

    ผลลัพธ์จะได้ Green
    นอกจากเราจะสามารถ tostring ให้ได้ค่า string เหมือนกับชื่อ enum แล้วนั้น ผู้พัฒนาทราบหรือไม่คะ ว่า enum เราสามารถกำหนดคำอธิบายได้เป็นประโยค และสามารถดึงค่านั้นมาใช้ได้ หรือพูดง่ายๆว่าเราสามารถดึงค่า enum มาใช้ในรูปแบบประโยคได้ง่ายๆ ดังตัวอย่างค่ะ

    public enum Colors
    {[Description(“This is Red”)]  //เราต้องกำหนด Description attributes ว่าเราต้องการจะให้แสดงข้อความอย่างไร
      Red = 10,   Green =20,
      Blue=30
    }

    ส่วนนี้จะเป็น code ส่วนที่จะดึงข้อมูล Description ตามที่กำหนด ที enum

    public static string GetEnumDescription(Enum value)

    {   

    FieldInfo fi = value.GetType().GetField(value.ToString());    

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(        typeof(DescriptionAttribute),        false);    

    if (attributes != null &&        attributes.Length > 0)       

    return attributes[0].Description;   

    else        return value.ToString();

    }

    ตัวอย่างการเรียกใช้

    var myEnumDescriptions = Colors.Red.GetDescription();

    ผลลัพธ์จะได้ This is Red

    ผู้เขียนหวังว่านี่คงจะเป็นอีกทางเลือกนึงของผู้พัฒนา ในการนำไปพัฒนาโปรแกรมต่อไปนะคะ ^_^

    แหล่งอ้างอิง
    https://stackoverflow.com/questions/2650080/how-to-get-c-sharp-enum-description-from-value
    http://yaritc.blogspot.com/2012/01/enum-struct-by-paedotnet.html