Compare Multiple Object

I Comparable Compare To(), How do I Compare Multiple Object Properties?

 

In your code

if (this.deg.CompareTo(newStudent.deg) == 1)
{
    // do something
}
else if (this.deg.CompareTo(newStudent.deg) != 1)
{
    // do something
}
else 
{
    // do something
}

else statement will never be reached, because result can either be equal to 1 or not. And you are checking only ‘deg’ value. For example, you can check if they are equal like this:

public int CompareTo(object obj)
{
    Student newStudent = obj as Student;
    // are equal
    if (deg.CompareTo(newStudent.deg) == 0 && 
        gra.CompareTo(newStudent.gra) == 0 &&
        lName.CompareTo(newStudent.lName) == 0 &&
        fName.CompareTo(newStudent.fName) == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here