我有班级:人,学生,老师
人员类别是:
public class Person
{
private string firstname;
private string lastname;
private string email;
public Person()
{
}
public int CompareTo(Person b)
{
return this.Email.CompareTo(b.Email);
}
public Person(string firstname, string lastname, string email)
{
this.Firstname = firstname;
this.Lastname = lastname;
this.Email = email;
}
public string Firstname{get;set;}
public string Lastname{get;set;}
public string Email{get;set;}
public virtual string GetDisplayText(string sep)
{
return Firstname + sep + Lastname + sep + Email;
}
}
学生班级:
public class Students : Person
{
private double assessmentGrade;
private double assignmentGrade;
public Students()
{
}
public Students(string firstname, string lastname,
double assessmentGrade,double assignmentGrade,
string email) : base(firstname, lastname, email)
{
this.AssessmentGrade = assessmentGrade;
this.AssignmentGrade = assignmentGrade;
}
public double AssignmentGrade{get;set;}
public double AssessmentGrade{get;set;}
public override string GetDisplayText(string sep)
{
return base.GetDisplayText(sep) + " " +
AssessmentGrade + " " + AssignmentGrade;
}
}
教师班:
public class Teacher : Person
{
private int roomNumber;
public Teacher()
{
}
public Teacher(string firstname, string lastname, int roomNumber,
string email) : base(firstname, lastname, email)
{
this.RoomNumber = roomNumber;
}
public int RoomNumber{get;set;}
public override string GetDisplayText(string sep)
{
return base.GetDisplayText(sep) + " , RoomNo " + RoomNumber;
}
}
我还有一个 PersonList 类:
public class PersonList : List<Person>
{
List<Person> person = new List<Person>();
public delegate void ChangeHandler(PersonList persons);
public event ChangeHandler Changed;
public bool Contains(string email)
{
return this[email] != null;
}
public new void Add(Person p)
{
base.Insert(0, p);
}
// Provide two additional methods
public void Fill()
{
List<Person> persons = PersonDB.GetPersons();
foreach (Person person in persons)
base.Add(person);
}
public void remove(Person persons)
{
person.Remove(persons);
}
public static PersonList operator +(PersonList persons, Person p)
{
persons.Add(p);
return persons;
}
public static PersonList operator -(PersonList persons, Person p)
{
persons.remove(p);
return persons;
}
public void Save()
{
PersonDB.SavePersons(this);
}
}
现在我正在尝试从 Form2 上传数据:
private PersonList persons = new PersonList();
private void button2_Click(object sender, EventArgs e)
{
int i = 0;
Form2 update = new Form2();
Person updteDtl = (Person)lstPerson.SelectedItem;
if (lstPerson.SelectedIndex != -1)
{
for (int j = 0; j < persons.Count; j++)
{
if (persons[j] == updteDtl)
{
i = j;
break;
}
}
update.UpdatingData = updteDtl;
Person updatePerson = update.GetNewPerson();
if (updatePerson != null)
{
persons[i] = updatePerson;
}
}
else
{
MessageBox.Show("Select the person to update.");
}
}
在 Form2 上,我正在尝试上传数据。
private Person person = null;
public Person GetNewPerson()
{
this.ShowDialog();
return person;
}
private Person updatingData = new Person();
public Person UpdatingData
{
set { updatingData = value; }
}
private void Form2_Load(object sender, EventArgs e)
{
if (updatingData is Students)
{
Students std = (Students)updatingData;
txtName.Text = std.Firstname;
txtLName.Text = std.Lastname;
txtEmail.Text = std.Email;
txtAssessGrade.Text = std.AssessmentGrade.ToString();
txtAssigGrade.Text = std.AssignmentGrade.ToString();
}
else if (updatingData is Teacher)
{
rbTeacher.Checked = true;
Teacher teacher = (Teacher)updatingData;
txtName.Text = teacher.Firstname;
txtLName.Text = teacher.Lastname;
txtEmail.Text = teacher.Email;
txtAssessGrade.Text = teacher.RoomNumber.ToString();
}
}
我想从我在列表框中选择的 Form2 中检索数据并更新该数据但是当我运行它并单击更新按钮时,它会向我显示一个错误无法将“System.String”类型的对象转换为“ProductMaint.Person” '。请告诉我如何解决它?