using System; using System.Collections; using System.Windows.Forms; namespace WindowsApplication5 { public partial class Form1 : Form { Collection collection1 = new Collection(); Collection collection2 = new Collection(); public Form1() { InitializeComponent(); } private void TurnIn_Click_1(object sender, EventArgs e) { // them vao collection collection1.Add(int.Parse(txtAge.Text), txtName.Text); lblStatus.Text = "Test " + txtAge.Text + " added."; } private void LookAt_Click_1(object sender, EventArgs e) { bool FirstCheck = collection2.Count() == 0; bool bThoat = false; while (collection1.Count() > 0 && !bThoat) { int x = collection1.Count() - 1; // copy phan tu cuoi cung cua collection1 thanh phan tu dau tien cua collection2 collection2.Insert(collection1.GetID(x), collection1.GetName(x), 0); // xoa phan tu cuoi cung cua collection1 collection1.RemoveAt(x); // kiem tra phan tu nay co la bai test dang tim if (collection2.GetID(0) == int.Parse(txtAge.Text)) { MessageBox.Show("Student Name is: " + collection2.GetName(0)); bThoat = true; } } if (!bThoat) // neu khong tim thay bai test nao if (FirstCheck) MessageBox.Show("Test not found."); else MessageBox.Show("Please click RETURN A TEST button to reset this result."); } private void Return_Click_1(object sender, EventArgs e) { // chuyen tat ca phan tu cua collection2 vao lai collection1 while (collection2.Count() > 0) { collection1.Add(collection2.GetID(0), collection2.GetName(0)); collection2.RemoveAt(0); } } } public class Collection : CollectionBase { public struct TuTao { public int Age; public string Name; } public void Add(int Age, string Name) { TuTao temp = new TuTao(); temp.Age = Age; temp.Name = Name; InnerList.Add(temp); } public void RemoveAt(int Vitri) { InnerList.RemoveAt(Vitri); } public void Insert(int Age, string Name, int Vitri) { TuTao temp = new TuTao(); temp.Age = Age; temp.Name = Name; InnerList.Insert(Vitri, temp); } public int Count() { return InnerList.Count; } public string GetName(int Vitri) { TuTao temp = (TuTao)InnerList[Vitri]; return temp.Name; } public int GetID(int Vitri) { TuTao temp = (TuTao)InnerList[Vitri]; return temp.Age; } } }
Demo 2 using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace CTDL_Chap01_Ex4 { class Program { static void Main(string[] args) { SinhVien A = new SinhVien(); A.Add("Le Thanh Dat"); A.Add("Vo Thuy Lien Tinh"); A.Add("Nguyen Thi My Hanh"); SinhVien B = new SinhVien(); B.Add("http://www.vnxsoft.com"); B.Add("http://www.microsoft.com"); Console.WriteLine("Truoc khi swap:"); for (int i = 0; i < A.Count(); i++) Console.WriteLine(A.GetName(i)); Console.WriteLine(); for (int i = 0; i < B.Count(); i++) Console.WriteLine(B.GetName(i)); Swap<SinhVien>(ref A, ref B); Console.WriteLine("\r\n\r\nSau khi swap:"); for (int i = 0; i < A.Count(); i++) Console.WriteLine(A.GetName(i)); Console.WriteLine(); for (int i = 0; i < B.Count(); i++) Console.WriteLine(B.GetName(i)); Console.ReadLine(); } public static void Swap<T>(ref T v1, ref T v2) { T temp = (T)v1; v1 = v2; v2 = temp; } } class SinhVien : CollectionBase { public void Add(object item) { InnerList.Add(item); } public string GetName(int Vitri) { return InnerList[Vitri].ToString(); } public int Count() { return InnerList.Count; } } }
1 comments:
Post a Comment