I have an array of objects that I'm trying to add to the Items collection of a combo box control using the AddRange method. The method takes an object[]
but when I pass it the name of the array which has been intialized with some values, it complains:
The best overloaded method match for
System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])
has some invalid arguments.
The class defining the objects in my array is very simple:
public class Action
{
public string name;
public int value;
public override string ToString()
{
return name;
}
}
and my array is declared such:
public Action[] actions = new Action[] {
new Action() { name = "foo", value = 1 },
new Action() { name = "bar", value = 2 },
new Action() { name = "foobar", value = 3 }
};
this is where I try to call AddRange
:
combobox1.Items.AddRange(actions);
and that's the line that it's complaining about - is there some step I'm missing to be able to do this? it works fine when I'm just adding a simple string[]
I tried it out in a .NET C# test project as below & it works fine. The sample code is as below:
public partial class Form1 : Form
{
public Action[] actions = new Action[]
{
new Action() { name = "foo", value = 1 },
new Action() { name = "bar", value = 2 },
new Action() { name = "foobar", value = 3 }
};
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(actions);
}
}
public class Action
{
public string name;
public int value;
public override string ToString()
{
return name;
}
}
So you need to tell us where have you declared the actions object.