Question

I am trying to use an ArrayList, and bind it to a BindingList...

If I try to put it in (I have Option Strict On), I get a suggestion for casting that I implemented -

Yet I keep getting a runtime error no matter what I try.

Unable to cast object of type 'ArrayList' to type 'IList`

The code:

Dim myBoundList As System.ComponentModel.BindingList(Of something) =
    New System.ComponentModel.BindingList(Of something)
      (CType(myArrayList, System.Collections.Generic.IList(Of something)))

I have tried to insert .ToArray...

The accepted answer at the link https://stackoverflow.com/a/8770832/1217150 does exactly the same thing (even though the intent is the opposite), and I have tried it... (I mean, creating an IList item, and assigning

IList iList = new ArrayList();

It gives me the same error...

I am using VB.NET, but c# would help too. Please help. Thank you.

Was it helpful?

Solution

You should check out using the generic List class (a .NET type) rather than using the non-generic ArrayList. I suspect this might be related to your problem. It's probably trying to cast the non-generic ArrayList to an IList<T> (the generic version of IList).

OTHER TIPS

If you are trying to add the array list as items to the listbox, you can use the following code:

private void Form1_Load(object sender, EventArgs e)
{
    // Create a new array list.
    ArrayList list = new ArrayList();

    // Add items to the array list
    list.Add("MyItem1");
    list.Add("MyItem2");
    list.Add("MyItem3");

    //Set the data source of the listbox as the array list
    listBox1.DataSource = list;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top