equal items -> Invalid Operation Exception

Oct 24, 2008 at 12:06 PM
This is the first time I try to get the ListControl running....
I have a Klass, which only instanciates an Object, which is a Collection of two strings.
Two columns - two strings...

So I have the following Code:



using System.Windows.Forms;
using BinaryComponents.SuperList;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public class StringCollection
        {
            private string string1;
            private string string2;

            public string String1
            {
                get { return string1; }
            }

            public string String2
            {
                get { return string2; }
            }

            public StringCollection(string string1, string string2)
            {
                this.string1 = string1;
                this.string2 = string2;
            }
        }

        public Form1()
        {
            InitializeComponent();

            Column columnString1 = new Column("String1", "String 1", 120, delegate(object item) { return ((StringCollection)item).String1; });
            Column columnString2 = new Column("String2", "String 2", 120, delegate(object item) { return ((StringCollection)item).String2; });

            columnString1.SortOrder = SortOrder.Descending;
            columnString2.SortOrder = SortOrder.Descending;

            listControl1.Columns.Add(columnString1);
            listControl1.Columns.Add(columnString2);

            listControl1.Items.Add(new StringCollection("string11", "string12"));           //Works fine
            listControl1.Items.Add(new StringCollection("string21", "string22"));           //Works fine
           
            //When I uncomment the following line, I get an "Invalid Operation Exception" in the Application.Run.
            //With the Text "Fehler beim Vergleichen von zwei Elementen im Array." (Error comparing two array-elements).
            //listControl1.Items.Add(new StringCollection("string11", "string12"));
        }      
    }
}



When I try to add two equal Items, I get an "Invalid Operation Exception" (see Code).
What am I doing wrong?

Version: 1.0.259.0
using Visual Studio 2008
Oct 26, 2008 at 6:33 PM
Hi,
I now tried a Debug Version of the dll and I now get stuck, when an Exception is thrown in "BufferedItemList.cs".
The following line throws the exception:
"throw new InvalidOperationException( "ItemList.ObjectComparer must be set" );"
The only usages I found sets the itemList to "this" so I don't know what to do...
Oct 26, 2008 at 9:00 PM
Yeah, I have it;)

I didn't realized, that the objects in the List must have an IComparable-Implementation. So this is a temporary solution:


public class StringCollection : IComparable
        {
            private string string1;
            private string string2;

            private static int indexer = 0;
            private int index;

            public string String1
            {
                get { return string1; }
            }

            public string String2
            {
                get { return string2; }
            }

            public StringCollection(string string1, string string2)
            {
                this.string1 = string1;
                this.string2 = string2;
                this.index = indexer;
                indexer++;
            }

            public int CompareTo(object obj)
            {
                return this.index - ((StringCollection)obj).index;  
            }
        }


Maybe, you can wrie the tutorial more "newbe-freindly"^^
Maybe, I write a tutorial for you, when I am ready with my Project to give you something back:)