what is the wrong with this implementation of Comparable<T>
- From: "Tony Johansson" <johansson.andersson@xxxxxxxxx>
- Date: Sun, 26 Oct 2008 16:21:52 GMT
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.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication15
{
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.WriteLine(pers.Age);
list.Sort();
foreach (Person pers in list)
Console.WriteLine(pers.Age);
}
}
public class Person : IComparable<Person>
{
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(Person other)
{
return this.Age - other.Age;
}
}
}
//Tony
.
- Follow-Ups:
- Re: what is the wrong with this implementation of Comparable<T>
- From: Jeroen Mostert
- Re: what is the wrong with this implementation of Comparable<T>
- Prev by Date: Re: Cross-platform GWT-like system for C#?
- Next by Date: Designer Structure Property
- Previous by thread: Deserialization
- Next by thread: Re: what is the wrong with this implementation of Comparable<T>
- Index(es):
Relevant Pages
|