Thứ Năm, 26 tháng 2, 2015

Mô hình MVP (Model - View - Presenter) C#

Mô hình MVP là gì vui lòng tham khảo: The Model-View-Presenter (MVP) Pattern

Demo mô hình MVP trong Windows Forms



Student Model:

    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string IdentityCardNumber { get; set; }
    }
 
 
Interface Add New Student
 
    public interface IAddStudent
    {
        string FirstName { get; set; }
        string LastName { get; set; }
        string IdentityCardNumber { get; set; }

        string Message { get; set; }
    }
 
View Add New Student:

 
Code Behind:
 
    public partial class AddStudent : Form, IAddStudent
    {
        public AddStudent()
        {
            InitializeComponent();
        }

        public string FirstName
        {
            get
            {
                return txtFirstName.Text;
            }
            set
            {
                txtFirstName.Text = value;
            }
        }

        public string LastName
        {
            get
            {
                return txtLastName.Text;
            }
            set
            {
                txtLastName.Text = value;
            }
        }

        public string IdentityCardNumber
        {
            get
            {
                return txtIdentityCardNumber.Text;
            }
            set
            {
                txtIdentityCardNumber.Text = value;
            }
        }

        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }
            set
            {
                _message = value;
                MessageBox.Show(_message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            StudentPresenter pre = new StudentPresenter(this);
            pre.AddStudent();
        }

    }
 
Student Presenter
 
    class StudentPresenter
    {
        IAddStudent addStudentView;

        public StudentPresenter(IAddStudent addStudentView)
        {
            this.addStudentView = addStudentView;
        }

        public bool AddStudent()
        {
            Student student = new Student();
            student.FirstName = addStudentView.FirstName;
            student.LastName = addStudentView.LastName;
            student.IdentityCardNumber = addStudentView.IdentityCardNumber;

            addStudentView.Message = string.Format("Thêm má»›i thành công sinh viên: {0} {1}", student.FirstName, student.LastName);
            //insert into db
            return true;
        }
    }

Test thử:

Thứ Tư, 11 tháng 2, 2015

Xóa bỏ phần tử trùng trong mảng c#

Một số cách nhanh để xóa bỏ phần tử trùng trong mảng:
- Sử dụng .Distinct()
- Lợi dụng nguyên tắc các Key của Dictionary không được trùng mình sẽ gán các phần tử vào tập Key này.
- Sử dụng HashSet.

Demo mảng số nguyên.

        static void Main(string[] args)
        {
            int[] arr = new int[1000];
            for (int i = 0; i < 998; i += 2)
            {
                arr[i] = i;
                arr[i + 1] = 1000 - i;
            }

            int[] result1 = arr.Distinct().ToArray();

            Dictionary<int, int> dic = new Dictionary<int, int>();
            HashSet<int> hash = new HashSet<int>();
            foreach (int i in arr)
            {
                dic[i] = 0;
                hash.Add(i);
            }

            int[] result2 = dic.Keys.ToArray();
            int[] result3 = hash.ToArray();


            Console.ReadLine();
        }