วันพฤหัสบดีที่ 12 มกราคม พ.ศ. 2555

การใช้งาน Enum และ Struct by paedotnet


การใช้งาน Enumeration,Struct
การใช้งาน 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

การใช้งาน switch กับ enum

using System;
public enum Colors
{
  Red = 10,
  Green =20,
  Blue=30
}
public class TestEnum
{
    public static void Main()
    {
        CallEnums(Colors.Red);
        Console.ReadLine();
    }
    public static void CallEnums(Colors c)
    {
        switch (c)
        {
            case Colors.Red: Console.WriteLine(" Red Color "); break;
            case Colors.Green: Console.WriteLine("Green Color"); break;
            case Colors.Blue: Console.WriteLine("Blue Color"); break;
            default: Console.WriteLine("invalid input"); break;
        }
    }
}

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

ตัวอย่างการใช้สมาชิกของ enum คำนวนค่า
using System;
public enum TestValue
{
     a = 10,
     b= 30,
     c= a+b
}
public class TestEnum
{
    public static void Main()
    {
        TestValue v = TestValue.c;
        Console.WriteLine((int)v);
        Console.ReadLine();
    }
}

ผลลัพธ์จะได้ 40
การวนลูปเพื่อแสดงสมาชิกของ enum

using System;
public enum Colors
{
    Red,Green,Blue,White,Black
}
public class TestEnum
{
    public static void Main()
    {
        Colors c;
        for (c = Colors.Red; c <= Colors.Black; c++) //วนลูปเพื่อแสดงสมาชิกแต่ละตัวของ Colors
        {
            Console.WriteLine("value is {0} ", c);
        }
        Console.ReadLine();
    }
}
ผลลัพธ์แสดงดังรูป

การใช้งาน GetValues() ใน enum

using System;
public enum Weeks
{
    Sun,Mon,Tue,Wed,Thr,Fri,Sat
}
public class TestEnum
{
    public static void Main()
    {
        Weeks w = Weeks.Sun;
        foreach (int i in Enum.GetValues(w.GetType())) //วนลูปเพื่อแสดงค่าสมาชิกแต่ละตัว
        {
            Console.WriteLine("value is {0}", Enum.GetName(w.GetType(), i)); //แสดงชื่อของสมาชิกแต่ละตัว
        }
        Console.ReadLine();
    }
}

 
ตัวอย่าง การใช้ GetNames()
using System;
public enum Weeks
{
    Sun,Mon,Tue,Wed,Thr,Fri,Sat
}
public class TestEnum
{
    public static void Main()
    {
        Weeks w = Weeks.Sun;
        foreach (string str in Enum.GetNames(w.GetType()))
        {
            Console.WriteLine("value is {0}",str);
        }
        Console.ReadLine();
    }
}
ผลลัพธ์แสดงดังรูป
การเปรียบเทียบค่าของสมาชิกใน enum

using System;
public enum AsciiCode
{
   A = 65,
   B =66,
   C=67,
   D =68
}
public class TestEnum
{
    public static void Main()
    {
        AsciiCode a = AsciiCode.C;
        AsciiCode b = AsciiCode.B;
        if (a < b) //เปรียบเที่ยบค่าของรหัส ASCII
        {
            Console.WriteLine("{0}  <  {1}", Convert.ToInt32(a),Convert.ToInt32(b));
        }
        else
        {
            Console.WriteLine("{0} > {1}", Convert.ToInt32(a),Convert.ToInt32(b));
        }
        Console.ReadLine();
    }
}

ผลลัพธ์ จะได้ 67 > 66

การตรวจสอบว่ามีสมาชิกตามที่เรากำหนดหรือไม่

using System;
public enum Colors
{
  Red,Green,Blue
}
public class TestEnum
{
    public static void Main()
    {
        if (Enum.IsDefined(typeof(Colors), "White"))  //ใช้ IsDefined ตรวจสอบสมาชิกของ enum
        {
            Console.WriteLine("Valid member");
        }
        else
        {
            Console.WriteLine("Invalid member");
        }
        Console.ReadLine();
    }
}

ผลลัพธ์จะได้ Invalid member เนื่องจาก enum ที่ชื่อ Colors ไม่มีสมาชิก White


การใช้งาน Struct
struct จะคล้ายๆกับ class ใช้สร้างกลุ่มข้อมูลต่างๆ เช่น เมธอด,properties,member variable
ข้อแตกต่างระหว่าง class กับ struct
1. Struct เป็น Value Type ส่วนคลาสเป็น Reference type
2. Struct ไม่สามารถใช้ Interitance
3.  สมาชิกของStruct ไม่สามารถระบุเป็น abstract,virtual
4.  Default Contructor จะถูกสร้างขึ้นมาทุกครั้งเมื่อสร้าง struct และ ค่า default Constructor จะไม่สามารถเปลี่ยนแปลงได้
5. Struct เราสามารถกำหนด Constructor ได้แต่ไม่สามารถกำหนด Destructor
รูปแบบ struct
struct struct_name{
//member ต่างๆ
}
struct เป็น keyword จะต้องประกาศทุกครั้งเมื่อสร้าง struct
struct_name เป็นชื่อของ struct
ตัวอย่าง

using System;
public struct TestColor
{
    public string mycolor;
    public TestColor(string color)
    {
        mycolor = color;
    }
    public string Color
    {
        get
        {
            return mycolor;
        }
        set
        {
            mycolor = value;
        }
    }
}
public class TestStruct
{
    public static void Main()
    {
        TestColor c = new TestColor("Green");
        Console.WriteLine(c.Color);
        c.Color = "Blue";
        Console.WriteLine(c.Color);
        Console.ReadLine();
    }
}


ตัวอย่าง

using System;
public struct TestColor
{
    public string mycolor;
}
public class TestStruct
{
    public static void Main()
    {
        TestColor c;
        c.mycolor = "White";
        Console.WriteLine(c.mycolor);
        Console.ReadLine();
    }
}

การใช้งานเมธอด ใน struct

using System;
public struct Rectangles
{
    public double width;
    public double height;
    public Rectangles(double width, double height)
    {
        this.width = width;
        this.height = height;
    }
    public double Size()
    {
        return width * height;
    }
}
public class TestStruct
{
    public static void Main()
    {
        Rectangles r = new Rectangles(4.4, 6.6);
        Console.WriteLine(r.Size());
        Console.ReadLine();
    }
}

 
การใช้งาน Generic struct

using System;
using System.Collections.Generic;
struct TestGeneric<T>
{
    public T value1, value2;
    public TestGeneric(T value1, T value2)
    {
        this.value1 = value1;
        this.value2 = value2;
    }
    public T Value1
    {
        get
        {
            return value1;
        }
        set
        {
            value1 = value;
        }
    }
    public T Value2
    {
        get
        {
            return value2;
        }
        set
        {
            value2 = value;
        }
    }
 }
public class TestStruct
{
    static void Main()
    {
        TestGeneric<int> a = new TestGeneric<int>(4, 8);
        Console.WriteLine(a.Value1 + "   " + a.Value2);
        a.Value1 = 10;
        a.Value2 = 20;
        Console.WriteLine(a.Value1 + "   " + a.Value2);
        Console.ReadLine();
    }
}
 

1 ความคิดเห็น: