Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:59:20+00:00 2026-06-05T23:59:20+00:00

I am trying to remove an object from an ArrayList and here is the

  • 0

I am trying to remove an object from an ArrayList and here is the code

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]); 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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:59:21+00:00Added an answer on June 5, 2026 at 11:59 pm

    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. Therefore, unless search.students returns the same instances of the objects as re.currentCourseStudents, it will never find a match and remove it.

    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). I say this because according to the MSDN, the ArrayList.Remove method uses Object.Equals to determin equality:

    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:

    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. If the lists contain only Student objects, then you should be using List<Student> or some other type-specific collection, if possible.

    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. If you choose to use the List<Student> type of list, its Remove method checks your objects for equality using IEquatable:

    public class Student : IEquatable<Student>
    {
        public bool Equals(Student other)
        {
            return (Student_Id == other.Studend_Id);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove an object from an ArrayList, My code is ArrayList
I'm trying to remove a class object from list<boost::any> l l.remove(class_type); I tried writing
I am trying to remove an object from the parent environment. rm_obj <- function(obj){
I am trying to remove the very last character from a TextCtrl object in
I'm trying to remove an object from (inside?) a object literal. But I cant
I'm trying to remove properties from an object if its name has a length
I have been trying to remove an object from the tableView I am working
I would like to remove an object from an ArrayList when I'm done with
I am trying to remove an object from a collection in entity framework, but
I am trying to remove an object from an array and fill the slot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.