CodeSmith Community
Your Code. Your Way. Faster!

InvalidCastException in ICollection.CopyTo when using a typed collection as DataSource (.NET/C#)

Latest post 07-03-2004 1:01 PM by dradovic. 3 replies.
  • 06-24-2004 9:08 AM

    • dradovic
    • Not Ranked
    • Joined on 06-24-2004
    • Posts 5
    • Points 110

    InvalidCastException in ICollection.CopyTo when using a typed collection as DataSource (.NET/C#)

    Hi all

    When I try to bind e.g. a combobox to a typed collection, I get an InvalidCastException. I've generated typed collections using ArrayList.cst and CSVector.cst but both showed the same behaviour.

    Can anyone help me? What am I missing?

    Cheers
    Dejan
    • Post Points: 60
  • 06-25-2004 11:06 AM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: InvalidCastException in ICollection.CopyTo when using a typed collection as DataSource (.NET/C#)

    Thanks for the bug report -- I don't use data-bound control so I would never have noticed.

    Could you please post a or e-mail me a minimal code sample that reproduces this bug? No need to attach the generated collection, of course; just tell me the data type you're using.

    Thanks, Chris
    • Post Points: 5
  • 06-28-2004 7:23 AM In reply to

    • dradovic
    • Not Ranked
    • Joined on 06-24-2004
    • Posts 5
    • Points 110

    RE: InvalidCastException in ICollection.CopyTo when using a typed collection as DataSource (.NET/C#)

    Ok, here we go.
    Create a simple test class as:

    public class Test
    {
    public string Text { get { return "TEST"; } }
    public Test()
    {
    }
    }

    Next create a typed collection using ArrayList.cst. Let's call it TestCollection.
    Then on a form try:

    TestCollection c = new TestCollection();
    c.Add(new Test());
    comboBox1.DataSource = c;
    comboBox1.DisplayMember = "Text";

    This will result in an InvalidCastException in ICollection.CopyTo(Array array, int arrayIndex).
    Copying the array manually will work. E.g. the following code works without throwing an exception:

    TestCollection c = new TestCollection();
    c.Add(new Test());
    Test[] arr = new Test[c.Count];
    c.CopyTo(arr);
    comboBox1.DataSource = arr;
    comboBox1.DisplayMember = "Text";

    Note that a the CSVector.cst template displays the same behaviour.

    Thank you very much,
    Dejan
    • Post Points: 5
  • 06-28-2004 8:07 AM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: InvalidCastException in ICollection.CopyTo when using a typed collection as DataSource (.NET/C#)

    Thanks for the sample code! I suspect there's some implicit assumption in the bowels of Windows.Forms that may be to blame here. I'll write up a test program and will report back when I know what happens.
     
    Edit: I was wrong, Windows.Forms databinding is behaving correctly. It's the ICollection.CopyTo methods that should accept an Object[] array but don't, because they want to call the strongly-typed CopyTo methods.
     
    That's a pretty nasty bug that requires some refactoring to clean in up in all collection classes. The next release will contain the fix.
     
    Cheers, Chris

    Post Edited (Chris Nahr) : 6/28/2004 9:35:09 AM GMT

    • Post Points: 5
  • 07-03-2004 1:01 PM In reply to

    • dradovic
    • Not Ranked
    • Joined on 06-24-2004
    • Posts 5
    • Points 110

    RE: InvalidCastException in ICollection.CopyTo when using a typed collection as DataSource (.NET/C#)

    Thanks a lot! It works well with the newest version.

    Cheers, Dejan
    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems