วันศุกร์ที่ 24 ตุลาคม พ.ศ. 2551

การใช้งาน Collections ใน .Net

การเขียนโปรแกรมส่วนใหญ่จะมีการใช้งาน Collections รวมด้วยเสมอเช่น การใช้งาน ListBox ก็จะมี Property Indexs หรือแม้แต่การใช้งาน DataSet ก็จะมี Tables, Columns, Rows ก็ล้วนเป็น Collections ด้วยกันทั้งสิ้น คราวนี้หากเราต้องการสร้าง Class ที่ต้องใช้ความสามารถดังกล่าว ตัวอย่าง การสร้างรายชื่อนักเรียนภายในห้อง เราก็จะสร้างคลาส ClassRoom ภายใน ClassRoom จะมี Property Students ซึ่งจะเก็บรายชื่อนักเรียนทั้งหมดไว้ ซึ่งจะมีโครงสร้างดังนี้

    Class จะมีทั้งหมด 3 class ด้วยกันคือ
  1. Student เป็นตัวแทนนักเรียนแต่ละคนในชั้นเรียน
  2. StudentList เป็นตัวแทนรายชื่อนักเรียนทั้งหมดในชั้นเรียน class นี้จะนำเอาเทคนิคการใช้ Collection มาใช้
  3. ClassRoom เป็นตัวแทนห้องเรียน แสดงการนำ Collection ที่ประการศไว้มาใช้ โดยนำคลาส StudentList มาเป็น Property หนึ่งของ ClassRoom

    ตัวอย่าง Code:

    public class Student
    {
        private int _id;
        private string _name;
        private string _surname;

        Student()
        {
            _id = 0;
        }
       
        public Student(int id, string name, string surname)
        {
            _id = id;
            _name = name;
            _surname = surname;
        }

        public int Id
        {
            get{ return _id; }
            set{ _id = id; }
        }

        public string Name
        {
            get{ return _name; }
            set{ _name = value;}
        }

        public string Surname
        {
            get{ return _surname; }
            set{ _surname = value; }
        }

        public override string ToString()
        {
            return _name + " " + _surname;
        }
    }

    จาก Student ข้างต้นจะมี attribute ที่เป็น private คือ _id, _name, _surname มี Properties 3 ตัวคือ Id, Name, Surname และมี Constructor 2 ตัว นอกจากนี้ยังมี method 1 ตัวคือ ToString() เพื่อให้สามารถแปลงเป็น string ได้เลย

    public class ClassRoom
    {
        public class StudentList : CollectionBase // ประกาศ Nested Class
        {
            public void Add(Student std)
            {
                if ((std.id == 0) || (std.Name == null) || (std.Surname == null))
                {
                    Console.WriteLine("Error: Student not initialize.");
                    return;
                }
                List.Add(std); //เพิ่ม std ลงใน list
            }

            public void Remove(Student std)
            {
                List.Remove(std); //ลบ std ออกจาก list
            }

            public Student this[int stdIndex] // ประกาศ property เพื่อให้สามารถมองในลักษณะ array
            {
                get { return (Student)List[stdIndex]; }
                set { List[stdIndex] = value; }
            }
        }

        private string _className;
        private string _Advisor;
        public StudentList Students;

        public ClassRoom()
        {
            Students = new StudentList(); // สร้างคลาส StudentList
        }

        public string ClassName
        {
            get { return _className; }
            set { _className = value; }
        }

        public string Advisor
        {
            get { return _Advisor; }
            set { _Advisor = value; }
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            str.Append("Class Room: " + _className + "\n");
            str.Append("Adviser: " + _Advisor + "\n");
            str.Append("Student: \n");
            int cnt = 1;
            foreach (Student std in Students)
            {
                str.Append("\t" + cnt++.ToString() + ") " + std + "\tid: " + std.id + "\n");
            }
            return str.ToString();
        }
    }

    จาก Code ข้างต้นภายในคลาส ClassRoom จะมีคลาสภายในคือ StudentList ซึ่งจะถือว่าเป็น Nested Class โดยที่ StduentList จะ Inherited Class มาจาก CollectionBase โดยภายใน CollectionBase จะมี property ที่สำคัญคือ List หลังจากที่ประกาศคลาสเรียบร้อยแล้ว ได้นำคลาส StudentList มาใช้กับ property Students

    public class Program
    {
        static void Main(string[] args)
        {
            ClassRoom c1 = new ClassRoom();
            c1.Advisor = "Tanapat Warahakit";
            c1.ClassName = "p1";
            c1.Students.Add(new Student(1001, "Tanapat", "Warahakit"));
            c1.Students.Add(new Student(1002, "Tanapong", "Warahakit"));
            c1.Students.Add(new Student(1003, "Jarutas", "Patthanapanchai"));
            Console.WriteLine(c1);
            Console.ReadKey();
        }
    }

    หลังจากที่ประกาศคลาส Student, StudentList และ ClassRoom เรียบร้อยแล้วก็ถึงเวลานำคลาสที่เตรียมไว้มาใช้ดังตัวอย่างข้างต้น

ไม่มีความคิดเห็น: