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
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
Comment