我有兩個類。一個叫做另一個學生。學生繼承人。 一切正常,直到我填寫我的表單並將數據傳遞到下一個表單進行顯示。繼承問題,從繼承類的某些數據獲得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.");
}
您正在使用哪個構造函數來實例'Student'?你是否使用具有所有參數的參數並將所有參數傳遞給基類?您是否嘗試設置斷點並調試應用程序? –
我正在實例化學生,然後單獨設置它們。我使用了調試器,它在所有提到的字段中都顯示爲空。我決定打破將學生列入學生名單的地方,並且在那裏他們已經爲空。 –
因此,你的問題是你的學生的價值觀沒有從你的輸入中正確地設置(即值應該不是'null'),還是在下一個表單上顯示的是空值?對不起,你的問題不清楚你的問題到底是什麼。 – monty