C#.NET程序设计第6章 集合、索引器实例6-3

设计一个Windows应用程序,定义一个Student类,包含姓名和学号两个字段,并定义一个班级类ClassList,该类包括一个Student集合,使用索引器访问该集合,实现与实例6-3相同的功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Student
        {
            string number;
            string name;
            public Student(string number, string name)
            {
                this.number = number;
                this.name = name;
            }
            public string Number
            {
                get { return number; }
            }
            public string Name
            {
                get { return name; }
            }
        }
        public class ClassList
        {
            private Student[] student;
            public ClassList(int x)
            {
                student = new Student[x];
            }
            public Student this[int index]
            {
                get
                {
                    if (index < 0 || index >= student.Length)
                    {
                        return null;
                    }
                    //copy from iotword.com
                    return student[index];
                }
                set
                {
                    if (index < 0 || index >= student.Length)
                    {
                        return;
                    }
                    student[index] = value;
                }
            }
            public Student this[string get]
            {
                get
                {
                    foreach (Student s in student)
                    {
                        if (s.Name.IndexOf(get) != -1 || s.Number.IndexOf(get) != -1)
                            return s;
                    }
                    return null;
                }
            }
        }
        ClassList a = new ClassList(50);

        private void button1_Click(object sender, EventArgs e)
        {
            Student s = a[textBox1.Text];
            if (s != null)
             //copy from iotword.com
                label4.Text = string.Format("找到学号为:{0}的同学的信息!\n学号:{1}!姓名:{2}", textBox1.Text, s.Number, s.Name);
            else
                label4.Text = string.Format("没有找到学号为:{0}的同学的信息", textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Student s = a[textBox2.Text];
            if (s != null)
                label4.Text = string.Format("找到姓名为:{0}的同学的信息!\n学号:{1}!姓名:{2}", textBox2.Text, s.Number, s.Name);
            else
                label4.Text = string.Format("没有找到姓名为:{0}的同学的信息", textBox2.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(textBox3.Text) - 1;
            Student s = new Student(textBox1.Text, textBox2.Text);
            a[i] = s;
           //copy from iotword.com
            label4.Text = string.Format("第{0}位同学信息添加成功!\n学号:{1}!姓名:{2}", i + 1, s.Number, s.Name);
        }


        private void button4_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(textBox3.Text) - 1;
            Student s = a[i];
            if (s != null)
                label4.Text = string.Format("第{0}位同学的信息为:\n学号:{1}姓名:{2}", i + 1, s.Number, s.Name);
            else
                label4.Text = string.Format("没有第{0}位同学的信息!", i + 1);
        }


    }









}




 

物联沃分享整理
物联沃-IOTWORD物联网 » C#.NET程序设计第6章 集合、索引器实例6-3

发表评论

提供最优质的资源集合

立即查看 了解详情