简体   繁体   English

从ArrayList中删除项目无法正常工作

[英]Remove item from ArrayList doesn't work properly

I am trying to remove an object from an ArrayList and here is the code 我试图从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() doesn't work even when I try to do the following arr[0].Equals(arstuedents[0]); this arr.Remove()即使在我尝试执行以下arr [0] .Equals(arstuedents [0])时也不起作用; it gives false every time I look to the values and IDs for the students of arr[0] and arstuedents[0] I found it the same but it gives false 每当我查看arr [0]和arstuedents [0]的学生的值和ID时,它都会给出错误。我发现它是相同的,但它给出了错误

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

What is the problem and why does the compiler not see it as equal? 问题是什么?为什么编译器看不到它是平等的?

//I did the following things //我做了以下事情

 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();
    }

}

//is that what you mean? //你是这个意思吗?

By default, the Remove method will only remove the exact instance being passed in. It will not remove another instance of the same type that just happens to be populated with the same values. 默认情况下, Remove方法只会删除传入的确切实例。它不会删除恰好使用相同值填充的同一类型的另一个实例。 Therefore, unless search.students returns the same instances of the objects as re.currentCourseStudents , it will never find a match and remove it. 因此,除非search.students返回的对象相同的实例作为re.currentCourseStudents ,它永远不会找到一个匹配,并删除它。

Either you need to search through arr for matches based on some unique property value and then remove it, or you will need to override the Equals method on that type (whatever type the objects are that are in that list). 您需要根据某些唯一属性值在arr搜索匹配项,然后将其删除,否则您将需要覆盖该类型上的Equals方法(该列表中的对象是什么类型)。 I say this because according to the MSDN, the ArrayList.Remove method uses Object.Equals to determin equality: 我这样说是因为根据MSDN, ArrayList.Remove方法使用Object.Equals来确定相等性:

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

For instance, if the objects are all Student objects, in your Student class, you would need to override the Equals method, as such: 例如,如果对象都是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);
}

Also, I would feel I have failed you in some way if I didn't mention the fact that the use of ArrayList should be discouraged. 另外,如果我没有提到不鼓励使用ArrayList的事实,我会觉得我在某种程度上让你失败了。 If the lists contain only Student objects, then you should be using List<Student> or some other type-specific collection, if possible. 如果列表仅包含Student对象,则应尽可能使用List<Student>或其他特定于类型的集合。

However, if your Student class inherits from a base class that seals the Equals method, such as DependencyObject , then you will not be able to override the equality check and therefore you must use a different type of list which checks for equality in a different way. 但是,如果您的Student类继承了密封Equals方法的基类,例如DependencyObject ,那么您将无法覆盖相等性检查,因此您必须使用不同类型的列表,以不同的方式检查相等性。 If you choose to use the List<Student> type of list, its Remove method checks your objects for equality using IEquatable : 如果选择使用List<Student>类型的列表,则其Remove方法使用IEquatable检查对象是否相等:

public class Student : IEquatable<Student>
{
    public bool Equals(Student other)
    {
        return (Student_Id == other.Studend_Id);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM