1

我正在尝试从 ArrayList 中删除一个对象,这是代码

Read re = new Read(connectionString);
List<Student> arcurrentCuourseStudnets= re.currentCourseStudents(); //Reading the students in this course it is return ArrayList with the IDs of all students in this course
List<Student> arstuedents=new List<Student>();
foreach (object ob1 in arcurrentCuourseStudnets)
{
      arstuedents.Add(re.student(((currentCourseStudents)ob1).StudentID.ToString()));//return the student as object indicates its ID FirstName .... 
}

listBoxSS.Items.Clear();
Read search = new Read(connectionString);
List<Student> arr = search.students();//Read all the students in DB


foreach (object ob in arstuedents)
{
    arr.Remove(ob); //remove the Current Course Students from the List to prevent the duplicate's 
}

this arr.Remove()即使我尝试执行以下操作也不起作用 arr[0].Equals(arstuedents[0]); 每次我查看 arr[0] 和 arstuedents[0] 的学生的值和 ID 时它都会给出 false 我发现它是一样的,但它给出了 false

foreach (object o in arr)
{
    listBoxSS.Items.Add((Student)o);
}

有什么问题,为什么编译器不认为它是平等的?

//我做了以下事情

 public class Student : IEqualityComparer<Student>
{
     int student_id;
     string first_name;
     string last_name;
     string mother_name;
     string father_name;
     DateTime birth_date;
     string education_level;
     string address;
     string notes;
     int[] phones;
    public Student(string first_name, string last_name, string mother_name, string father_name, DateTime birth_date, string education_level, string address, string notes)
    {
        this.first_name = first_name;
        this.last_name = last_name;
        this.mother_name = mother_name;
        this.father_name = father_name;
        this.birth_date = birth_date;
        this.education_level = education_level;
        this.address = address;
        this.notes = notes;
    }
    public Student(int student_id, string first_name, string last_name, string mother_name, string father_name, DateTime birth_date, string education_level, string address, string notes)
    {
        this.first_name = first_name;
        this.last_name = last_name;
        this.mother_name = mother_name;
        this.father_name = father_name;
        this.birth_date = birth_date;
        this.education_level = education_level;
        this.address = address;
        this.notes = notes;
        this.student_id = student_id;
    }
    public int Student_id
    { get { return student_id; } }
    public string First_name
    { 
        get
        { return first_name; }
        set
        { first_name = value; }
    }
    public string Last_name
    { 
        get 
        { return last_name; }
        set
        { last_name = value; }
    }
    public string Mother_name
    { 
        get { return mother_name; }
        set
        { mother_name = value; }
    }
    public string Father_name
    { 
        get { return father_name; }
        set
        { mother_name = value; }
    }
    public DateTime Birth_date
    { 
        get { return birth_date; }
        set
        { birth_date = value; }
    }
    public string Education_level
    { 
        get { return education_level; }
        set
        { education_level = value; }
    }
    public string Address
    { 
        get { return address; }
        set
        { education_level = value; }
    }
    public string Notes
    { 
        get { return notes; }
        set
        { notes = value; }
    }
    public int[] Phones
    {
        get { return phones; }
        set { phones = value; }
    }
    public override string ToString()
    {
        if (phones != null && phones[0] != 0)
            return first_name.PadRight(30, ' ') + father_name.PadRight(30, ' ') + last_name.PadRight(30, ' ') + phones[0].ToString();
        else
            return first_name.PadRight(30, ' ') + father_name.PadRight(30, ' ') + last_name;
    }

    public bool Equals(Student x, Student y)
    {
        return (x.Student_id == y.Student_id);
    }

    public int GetHashCode(Student obj)
    {
        return obj.GetHashCode();
    }

}

//你是这个意思吗?

4

1 回答 1

3

默认情况下,该Remove方法只会删除传入的确切实例。它不会删除恰好填充了相同值的相同类型的另一个实例。因此,除非search.students返回与 相同的对象实例,否则re.currentCourseStudents它永远不会找到匹配项并将其删除。

您需要根据某些唯一属性值搜索arr匹配项,然后将其删除,或者您需要覆盖该类型的 Equals 方法(无论该列表中的对象是什么类型)。我这样说是因为根据 MSDN,该ArrayList.Remove方法用于Object.Equals确定相等性:

http://msdn.microsoft.com/en-us/library/system.collections.arraylist.remove.aspx

例如,如果对象都是Student对象,则在您的Student类中,您需要重写该Equals方法,如下所示:

public override bool Equals(object obj)
{
    Student other = obj as Student;
    if (other != null)
        return (obj.Id == this.Id);
    else
        return base.Equals(obj);
}

另外,如果我没有提到不鼓励使用 of 的事实,我会觉得我在某种程度上让你ArrayList失望了。如果列表仅包含Student对象,那么您应该使用List<Student>或其他一些特定于类型的集合(如果可能)。

但是,如果您的Student类继承自密封方法的基类Equals,例如DependencyObject,那么您将无法覆盖相等性检查,因此您必须使用不同类型的列表以不同方式检查相等性。如果您选择使用List<Student>列表类型,它的方法会使用以下Remove方法检查您的对象是否相等IEquatable

public class Student : IEquatable<Student>
{
    public bool Equals(Student other)
    {
        return (Student_Id == other.Studend_Id);
    }
}
于 2012-06-18T13:31:35.943 回答