2013-12-10 50 views
1

我有两个类。一个叫做另一个学生。学生继承人。 一切正常,直到我填写我的表单并将数据传递到下一个表单进行显示。继承问题,从继承类的某些数据获得NULL

除了名称,地址和状态,一切都显示正常。这些来自person类并具有NULL值。让我疯狂的是,城市,邮政编码和出生日期来自同一个人班,工作得很好。我不明白为什么会发生这种情况,并希望有更多知识的人能指引我走向正确的方向。

这里有2类:

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."); 
    } 
+1

您正在使用哪个构造函数来实例'Student'?你是否使用具有所有参数的参数并将所有参数传递给基类?您是否尝试设置断点并调试应用程序? –

+0

我正在实例化学生,然后单独设置它们。我使用了调试器,它在所有提到的字段中都显示为空。我决定打破将学生列入学生名单的地方,并且在那里他们已经为空。 –

+0

因此,你的问题是你的学生的价值观没有从你的输入中正确地设置(即值应该不是'null'),还是在下一个表单上显示的是空值?对不起,你的问题不清楚你的问题到底是什么。 – monty

回答

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

看到二传手 - 你这里所做的是破坏你的name没有设置address。这应该解释你所有的数据损坏。由于您在代码中首先设置了name,这解释了为什么它们都是null

+0

是...复制/粘贴赢(丢失)再次:) – DRapp

+0

当我没有看到。谈论没有看到树木与森林的方式。谢谢 –

2

与解决的问题是,在一组,你把它作为name = address而不是address = value;

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