T
Tony Johansson
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
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