problem arraylist sorting: InvalidOperationException

Hi, I'm just learning C#. Today, I encountered relatively special problem. I tried to sort an arraylist which contains my own class type . Everything works well but when I add a string to the arraylist the complier produce : InvalidOperationException at run time.

in implementing compareTo for my class I write some line to deal with strings but I have no access to string class of C# and so I can not change its compareto method too.
Is there anyway to deal with this problem?

thank u

Comments

  • Build a comparer:

    [code]
    public class MyComparer : Comparer
    {
    public override int Compare(object x, object y)
    {
    if (x is MyClass && y is string)
    return ((MyClass)x).MyValue.CompareTo((string)y);
    if (y is MyClass && x is string)
    return ((MyClass)y).MyValue.CompareTo((string)x);
    var cmpX = x as IComparable;
    var cmpY = y as IComparable;
    if (cmpX == null || cmpY == null)
    throw new Exception("Can't Compare");
    return cmpX.CompareTo(cmpY);
    }
    }
    [/code]

    I haven't actually run that to make sure it works, but that will be the basic idea...

    Then in your array list you pass your comparer to the sort function:
    [code]
    MyComparer comp = new MyComparer();
    myArrayList.Sort(comp);
    [/code]
  • Thank you, but I don't how your code should work? C# do not recognize "var" at all.
    here is my code:


    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;

    namespace colltest
    {


    class student:IComparable
    {
    string name;
    int id;


    public override bool Equals(object obj)
    {
    student r = (student)obj;
    return this.name.Equals(r.name)&& this.id.Equals(r.id);
    }


    public int CompareTo(object rhs)
    {
    student r = rhs as student;
    if (r!=null){
    return this.name.CompareTo(r.name);
    }


    }
    public student()
    {
    name = "";
    id = 0;
    }

    public student(string name, int id)
    {
    this.name = name;
    this.id = id;
    }
    public string Name
    {
    get{
    return name;
    }
    set{
    name = value;
    }
    }

    public int ID
    {
    get
    {
    return id;
    }
    set
    {
    id = value;
    }
    }
    public override string ToString()
    {
    return ("Name: "+name+"
    ID: "+id);
    }
    }


    class Program
    {
    static void Main(string[] args)
    {


    ArrayList stulist = new ArrayList();
    stulist.Add(new student("z",1));

    stulist.Add("hello there");

    stulist.Insert(2,new student("x",2));


    // try
    {
    stulist.Sort();
    }

    // catch (InvalidOperationException e)
    {

    }


    for (int i = 0; i < stulist.Count; i++)
    {
    // if (stulist[i] is string)
    {
    Console.WriteLine("{0}:
    {1}",i,stulist[i]);
    }
    }


    Console.ReadKey();
    }
    }
    }
  • var was added in .net 3.0 I believe.

    replace "var" with "IComparable"
    replace "MyClass" with "student"
    replace "MyValue" with "Name"

    I think that should do it.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories