建立了一个用户类:
class clsStudent
    {
private string _strName;        public string StrName
        {
            get { return _strName; }
            set { _strName = value; }
        }        private string _strSex;        public string StrSex
        {
            get { return _strSex; }
            set { _strSex = value; }
        }        private string _strAge;        public string StrAge
        {
            get { return _strAge; }
            set { _strAge = value; }
        }
}程序界面设计:
逐个录入该用户信息,存储到实例化的用户类中,并且将该实例化类存储到数组中。
添加信息时如果勾选了CheckBox,则对其性别分两次存入数组。
问题就在一旦勾选CheckBox,则数组中以前存储的性别信息就会被最后一次的性别信息覆盖。
代码如下:
ArrayList al;        private void Search()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("姓名", typeof(string));
            dt.Columns.Add("年龄", typeof(string));
            dt.Columns.Add("性别", typeof(string));
            
            for (int i = 0; i < al.Count; i++)
            {
                DataRow dr = dt.NewRow();
                Student tmp = (Student)al[i];
                string x1 = tmp.StrName;
                string x2 = tmp.StrAge;
                string x3 = tmp.StrSex;
                dr[0] = x1;
                dr[1] = x2;
                dr[2] = x3;
                dt.Rows.Add(dr);
            }
            dataGridView1.DataSource = dt;
        }
        private void AddData(Student st)
        {
            if (al == null) al = new ArrayList();
            al.Add(st);
        }        private void button1_Click(object sender, EventArgs e)
        {
            
            Student stu1 = new Student();
            stu1.StrAge = textBox2.Text.Trim();
            stu1.StrName = textBox1.Text.Trim();
            if (checkBox1.Checked)
            {
                stu1.StrSex = "男";
                AddData(stu1);                stu1.StrSex = "女";
                AddData(stu1);
            }
            else
            {
                AddData(stu1);
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            Search();
        }程序设计需要一个CheckBox做判断,但是不知道如何解决
不勾选CheckBox的运行效果如下:勾选CheckBox的运行效果如下:请大家帮帮忙,小弟新手,谢谢了

解决方案 »

  1.   

    ArrayList al;
    这个是static的是吧,如果是继续往下看。
    Student stu1 = new Student();
                stu1.StrAge = textBox2.Text.Trim();
                stu1.StrName = textBox1.Text.Trim();
                if (checkBox1.Checked)
                {
                    stu1.StrSex = "男";
                    AddData(stu1);                stu1.StrSex = "女";
                    AddData(stu1);
                }
                else
                {
                    AddData(stu1);
                }
    问题关键出在这,一个实例虽然加入了数组,但是你他的引用更改了数据。肯定会把原来的覆盖掉的。
      

  2.   

    class clsStudent
        {
    private string _strName;        public string StrName
            {
                get { return _strName; }
                set { _strName = value; }
            }        private string _strSex;        public string StrSex
            {
                get { return _strSex; }
                set { _strSex = value; }
            }        private string _strAge;        public string StrAge
            {
                get { return _strAge; }
                set { _strAge = value; }
            }
    }
    =>
    class clsStudent:ICloneable
        {
    private string _strName;        public string StrName
            {
                get { return _strName; }
                set { _strName = value; }
            }        private string _strSex;        public string StrSex
            {
                get { return _strSex; }
                set { _strSex = value; }
            }        private string _strAge;        public string StrAge
            {
                get { return _strAge; }
                set { _strAge = value; }
            }            object ICloneable.Clone()
                {
                  return base.MemberwiseClone();
                }
    }
    ----------------------------------------stu1.StrSex = "女";
                    AddData(stu1);=>Student stu2 = stu1.Clone();stu2.StrSex = "女";
                    AddData(stu2);
      

  3.   

    感谢sunzongbao2007、Sandy945,终于搞明白了是肿么回事。学习了。
    类实例化赋值后,动态存储后是不能修改值的。
    谢谢了!!
    图片是发到百度空间的,不知道怎么回事一会儿就看不到了