what is the wrong with this implementation of Comparable<T>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    what is the wrong with this implementation of Comparable<T>

    Hello!

    I have the following simple program below.
    What is the problem when I get runtime error for
    "Failed to compare two elements in the array ?"

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Collecti ons;

    namespace ConsoleApplicat ion15
    {
    class Program
    {
    static void Main(string[] args)
    {
    ArrayList list = new ArrayList();
    list.Add(new Person("tony", 13));
    list.Add(new Person("olle", 23));
    list.Add(new Person("stina", 53));
    list.Add(new Person("august" , 3));
    list.Add(new Person("roland" , 33));

    foreach (Person pers in list)
    Console.WriteLi ne(pers.Age);

    list.Sort();
    foreach (Person pers in list)
    Console.WriteLi ne(pers.Age);
    }
    }

    public class Person : IComparable<Per son>
    {
    string name;
    int age;

    public Person(string lname, int lage)
    {
    name = lname;
    age = lage;
    }

    public int Age
    {
    set {age = value;}
    get {return age; }
    }

    public int CompareTo(Perso n other)
    {
    return this.Age - other.Age;
    }
    }
    }


    //Tony


  • Jeroen Mostert

    #2
    Re: what is the wrong with this implementation of Comparable&lt;T &gt;

    Tony Johansson wrote:
    I have the following simple program below.
    What is the problem when I get runtime error for
    "Failed to compare two elements in the array ?"
    >
    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Collecti ons;
    >
    namespace ConsoleApplicat ion15
    {
    class Program
    {
    static void Main(string[] args)
    {
    ArrayList list = new ArrayList();
    You're using ArrayList...
    public class Person : IComparable<Per son>
    But you fail to implement the non-generic interface IComparable.

    If you use List<Personinst ead of ArrayList (which is a good idea), the
    problem disappears, but you should also implement the non-generic
    counterpart to IComparable<T>.
    public int CompareTo(Perso n other)
    {
    return this.Age - other.Age;
    }
    This implementation is not correct: you must check for the case that "other"
    is null (and return a positive value in that case).

    --
    J.

    Comment

    • Tony Johansson

      #3
      Re: what is the wrong with this implementation of Comparable&lt;T &gt;

      Hello!

      Yes I noticed that my runtime error disapperar when I used List<Tinstad of
      ArrayList.
      Yes I also know that it's a better solution to use List<Tinstad of
      ArrayList.

      But I just want to find out what is causing the error when I use the
      ArrayList?

      //Tony


      "Jeroen Mostert" <jmostert@xs4al l.nlskrev i meddelandet
      news:49049c5d$0 $192$e4fe514c@n ews.xs4all.nl.. .
      Tony Johansson wrote:
      >I have the following simple program below.
      >What is the problem when I get runtime error for
      >"Failed to compare two elements in the array ?"
      >>
      >using System;
      >using System.Collecti ons.Generic;
      >using System.Text;
      >using System.Collecti ons;
      >>
      >namespace ConsoleApplicat ion15
      >{
      > class Program
      > {
      > static void Main(string[] args)
      > {
      > ArrayList list = new ArrayList();
      >
      You're using ArrayList...
      >
      > public class Person : IComparable<Per son>
      >
      But you fail to implement the non-generic interface IComparable.
      >
      If you use List<Personinst ead of ArrayList (which is a good idea), the
      problem disappears, but you should also implement the non-generic
      counterpart to IComparable<T>.
      >
      > public int CompareTo(Perso n other)
      > {
      > return this.Age - other.Age;
      > }
      >
      This implementation is not correct: you must check for the case that
      "other" is null (and return a positive value in that case).
      >
      --
      J.

      Comment

      • Peter Duniho

        #4
        Re: what is the wrong with this implementation of Comparable&lt;T &gt;

        On Sun, 26 Oct 2008 10:15:01 -0700, Tony Johansson
        <johansson.ande rsson@telia.com wrote:
        Hello!
        >
        Yes I noticed that my runtime error disapperar when I used List<T>
        instad of
        ArrayList.
        Yes I also know that it's a better solution to use List<Tinstad of
        ArrayList.
        >
        But I just want to find out what is causing the error when I use the
        ArrayList?
        He told you. You've only implemented IComparable<Per son>, not
        IComparable. He also pointed out a mistake in your comparison method,
        which you should also fix.

        For future reference, while it's very good that you included the error
        message in your question, you should also be specific about _where_ the
        message happens. In this case, it's relatively obvious, but you should
        get into the habit of being as specific as possible when asking questions.

        Pete

        Comment

        • Tony Johansson

          #5
          Re: what is the wrong with this implementation of Comparable&lt;T &gt;

          Hello!

          Here is the new code implementing IComparable<Tan d IComparable.
          If I use List<Tit works fine.
          If I instead use ArrayList I get runtime error with the message
          "Failed to compare two elements in the array ?"
          when the List.Sort() is called.

          I just want to find the problem tom this runtime error ?

          using System.Collecti ons.Generic;
          using System.Text;
          using System.Collecti ons;

          namespace ConsoleApplicat ion15
          {
          class Program
          {
          static void Main(string[] args)
          {
          ArrayList list = new ArrayList();
          list.Add(new Person("tony", 13));
          list.Add(new Person("olle", 23));
          list.Add(new Person("stina", 53));
          list.Add(new Person("august" , 3));
          list.Add(new Person("roland" , 33));
          list.Sort();
          foreach (Person pers in list)
          Console.WriteLi ne(pers.Age);
          }
          }

          public class Person : IComparable<Per son>
          {
          string name;
          int age;

          public Person(string lname, int lage)
          {
          name = lname;
          age = lage;
          }

          public int Age
          {
          set {age = value;}
          get {return age; }
          }

          public int CompareTo(Perso n other)
          {
          if (other ==null)
          return 1;
          return this.Age - other.Age;
          }

          public int CompareTo(objec t obj)
          {
          if (obj is Person)
          {
          Person pers = obj as Person;
          return this.Age - pers.Age;
          }
          else
          {
          if (obj == null)
          return 1;
          throw new ArgumentExcepti on("Object to compare to is not a
          Person object");
          }
          }
          }
          }

          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms krev i meddelandet
          news:op.ujm6zhp e8jd0ej@petes-computer.local. ..
          On Sun, 26 Oct 2008 10:15:01 -0700, Tony Johansson
          <johansson.ande rsson@telia.com wrote:
          >
          >Hello!
          >>
          >Yes I noticed that my runtime error disapperar when I used List<T>
          >instad of
          >ArrayList.
          >Yes I also know that it's a better solution to use List<Tinstad of
          >ArrayList.
          >>
          >But I just want to find out what is causing the error when I use the
          >ArrayList?
          >
          He told you. You've only implemented IComparable<Per son>, not
          IComparable. He also pointed out a mistake in your comparison method,
          which you should also fix.
          >
          For future reference, while it's very good that you included the error
          message in your question, you should also be specific about _where_ the
          message happens. In this case, it's relatively obvious, but you should
          get into the habit of being as specific as possible when asking questions.
          >
          Pete

          Comment

          • Jeroen Mostert

            #6
            Re: what is the wrong with this implementation of Comparable&lt;T &gt;

            Tony Johansson wrote:
            Here is the new code implementing IComparable<Tan d IComparable.
            It's not actually doing that.
            public class Person : IComparable<Per son>
            This should be

            public class Person : IComparable<Per son>, IComparable

            You must explicitly mention an interface to implement it.

            Also, because the non-generic method is strictly less useful than its
            generic (strongly typed) counterpart, you should implement it explicitly to
            prevent it from being called directly:

            int IComparable.Com pareTo(object obj) {
            if (obj == null) return 1;
            if (obj.GetType() != typeof(Person)) throw new ArgumentExcepti on();
            return this.CompareTo( (Person) obj);
            }

            --
            J.

            Comment

            Working...