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();
}
}
// ということですか?