遗传问题,从继承类的某些数据获取NULL

时间:2013-12-10 02:43:59

标签: c# winforms inheritance

我有两节课。一个叫另一个学生。学生继承人。 一切正常,直到我填写表格并将数据传递到下一个表格进行展示。

除名称,地址和州外,一切都很好。这些来自person类并具有NULL值。让我疯狂的是,城市,邮政编码和生日来自同一个人的班级,工作得很好。我不知道为什么会这样,希望有更多知识的人能指出我正确的方向。

以下是两个班级:

public class Person
{
    private string name;
    private string address;
    private string city;
    private string state;
    private string zipcode;
    private DateTime birthdate;

    public Person()
    {
    }

    public Person(string name, string address, string city, string state, string zipcode, DateTime birthdate)
    {
        this.name = name;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipcode = zipcode;
        this.birthdate = birthdate;
    }

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

    public string Address
    {
        get { return address; }
        set { name = address; }
    }

    public string City
    {
        get { return city; }
        set { city = value; }
    }
    public string State
    {
        get { return state; }
        set { state = value; }
    }

    public string Zipcode
    {
        get { return zipcode; }
        set { zipcode = value; }
    }

    public DateTime Birthdate
    {
        get { return birthdate; }
        set { birthdate = value; }
    }
}

public class Student : Person
{
    private string studentID;
    private double gradepointAverage;
    private List<string> currentClasses = new List<string>();

    public Student()
    {
    }

    public Student(string name, string address, string city, string state, string zipcode, DateTime birthdate, string studentID, double gradepointAverage, List<string> currentClasses)
        : base(name, address, city, state, zipcode, birthdate)
    {
        this.studentID = studentID;
        this.gradepointAverage = gradepointAverage;
        this.currentClasses = currentClasses;
    }

    public string StudentID
    {
        get { return studentID; }
        set { studentID = value; }
    }

    public double GradePointAverage
    {
        get { return gradepointAverage; }
        set { gradepointAverage = value; }
    }

    public List<string> CurrentClasses
    {
        get { return currentClasses; }
        set { currentClasses = value; }
    }
}

这是我实例化类的地方:

private void btnSave_Click(object sender, EventArgs e)
    {
        s = new Student();
        s.StudentID = txtStudentID.Text;
        s.Name = txtName.Text;
        s.Address = txtAddress.Text;
        s.City = txtCity.Text;
        s.State = txtState.Text;
        s.Zipcode = txtZipCode.Text;
        s.Birthdate = DateTime.Parse(txtBirthdate.Text);
        s.GradePointAverage = Convert.ToDouble(txtGradPoint.Text);


        string stringItmem;
        foreach(Object selectedItem in lstClasses.SelectedItems)
        {
            stringItmem = selectedItem as String;
            s.CurrentClasses.Add(stringItmem);
        }
        students.Add(s);
        resetForm();
        MessageBox.Show("Student has been saved.");
    }

2 个答案:

答案 0 :(得分:3)

public string Address
{
    get { return address; }
    set { name = address; }   // Should be "address = value"
}

查看设置者 - 您在此处执行的操作会在不设置name的情况下损坏您的address。这应该解释您的所有数据损坏。由于您在代码中首先设置了name,因此可以解释为什么它们都是null

答案 1 :(得分:2)

您的地址存在的问题是,在地址中您将其设为name = address而不是address = value;

public string Address
{
    get { return address; }
    set { name = address; }
}