Alexev
Alexev

Reputation: 157

C# IComparable with one parent and 2 children

Well, I have a class Human that implement IComparable<Human>

Then I have two more classes that inherit from Human Child:Human and Cousin:Human

The Parent class has a property AGE that has in the getter a call to a function getAge() which is abstract.

I have a List of Humans and when I display them in a datagrid every age is calculated properly.

I want to sort the list using age as attribute so I make the Human abstract class to implement Icomparable and then the method like this.

public int CompareTo(Human other)
{
     return this.age.CompareTo(other.age);
}

I invoke the list.sort() method in the ASP like this

List<Human> hlist = instance.humanlist;
hlist.Sort();
tblHumans.DataSource = hlist;
tblHumans.DataBind();

The page loads with all the data but the items are not ordered by age, it seems ordered by position in the list.

My tblHumans is

<asp:GridView ID="tblHumans" runat="server">
</asp:GridView>

In the Parent class the attribute AGE is like this

public int Age
{
    get
    {
        return getAge();
    }

    set
    {
        age = getAge();
    }
}

getAge() is an abstract method that my child classes overrides

The calculation is returns values correctly, when the table is rendered every single value is there with the right results.

What I am doing wrong?

Upvotes: 1

Views: 379

Answers (3)

Thomas Levesque
Thomas Levesque

Reputation: 292565

Short answer

public int CompareTo(Human other)
{
     return this.Age.CompareTo(other.Age);
}

(Age, not age)

Longer answer

Your implementation of the Age property is broken. You have an age field, but its value is not used by the getter (which just calls getAge()). And the setter ignores the implicit value argument, so it just assigns age to the result of getAge(). So, while the setter hasn't been called, age isn't initialized and its value is 0. You probably shouldn't have a setter at all, and you should remove the age field, since the value of Age is only decided by the implementation of getAge().

Upvotes: 3

Alexev
Alexev

Reputation: 157

Ok, I fix it

thank you all for the time you took reading and answering this question.

At last I made this:

1) set again the age property and field with normal getter and setter 2) in the child class when I override the getAge method I force set the age too, so, when the compareTo is called in the sort method of the list the age property is populated so it is shown in the table.

I know it is not 'the best practic' but It has to be made this way because in this program I cannot use 'complex' data models (not even a database, but dont worry is not for a production system)

Thanks all again

Upvotes: 0

Jodrell
Jodrell

Reputation: 35726

Debug your problem like this,

List<Human> hlist = instance.humanlist;
hlist.Sort();

// What is the order of the elements here, have they been sorted as you expect?    

tblHumans.DataSource = hlist;
tblHumans.DataBind();

If the list is sorted as you expect, then the problem is with the grid, if not, then you haven't implemented IComparable or the Age property properly. Which is it?

Upvotes: 0

Related Questions