-3

我有班级:人,学生,老师

人员类别是:

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” '。请告诉我如何解决它?

4

2 回答 2

1

我假设这一行是引发异常的行:

Person updteDtl = (Person)lstPerson.SelectedItem;

换句话说, in 中的选定项目lstPerson是一个字符串,而不是 a Person,因此您不能将其强制转换为 a Person

不幸的是,尽管您向我们展示了数百行代码,但您没有告诉我们lstPerson或为什么您希望 lstPerson.SelectedItem成为Person. (你甚至没有告诉我们你正在编写什么样的应用程序。)

这就是你的问题的原因,它与你发布的代码几乎没有关系——所以专注于那部分。要么更改绑定到列表的方式,要么根据SelectedItem.

于 2012-08-18T19:29:34.423 回答
0

我的猜测是问题出在这一行:

Person updteDtl = (Person)lstPerson.SelectedItem;

显然这返回一个字符串,而不是一个Person对象。我实际上不确定是否可以Person在下拉列表中存储一个对象,例如一个实例(我假设这是什么lstPerson?)。

一种解决方案可能是使用其中一个字段来识别每个人,并将其用作下拉列表中项目的值(例如,电子邮件可能对每个人都是唯一的?否则,PersonId为只是这个目的)。由于下拉列表中的每个项目都可以同时value显示一个字段和一个文本字符串,因此您可以使用 ID 作为值,并使用名称(或任何您想要的名称)作为显示文本。

在事件处理程序中,您必须获取value,并使用它来查找Person列表中的人员。然后,您可以使用表单中的其余数据更新该人。

我希望我正确理解了您的问题,并且这很清楚可以提供一些帮助,祝您好运!

于 2012-08-18T19:44:04.397 回答