我有一个类型为Product
的列表。 。 。使用循环从对象列表中填充项目数组
public List<Product> products = new List<Product>();
。 。 。并且我想创建一个方法GetList(string theType)
,如果该方法提供的theType
参数与List
中的任何对象内的Type
字段匹配,该方法将使用该List
中的项目填充数组。
只有我想返回时,数组包含的东西是所有那些反对提供theType
参数匹配成功的产品名。
public string[] GetList(string theType)
{
string[] theList = new string[10];
for(int i = 0; i < theList.Length; i++)
{
foreach (Product p in products)
{
if (p.Type.Equals(theType))
{
theList[i] = p.ProductName;
}
}
}
return theList;
}
这似乎并不奏效。即使我可以看到它。我太累了想不出来。
编辑:
我想填充ComboBox与返回theList
。 有两个组合框。您必须在第一个选项中选择一个预设值以启用第二个预设值,并且应该使用combobox1中选择的产品类型填充第二个预设值。我只有一个事件处理对于ComboBox:
private void combobox1_SelectedValueChanged(object sender, EventArgs e)
{
if (combobox1.Text != "")
{
combobox2.Enabled = true;
combobox2.Items.Clear();
if (combobox1.SelectedText.Equals("Dairy"))
{
// i try to display what the method has returned inside a messagebox, but it doesn't display it at all, the messagebox
string[] theList = client.GetList("dairy");
string theStringList = "";
for (int i = 0; i < theList.Length; i++)
{
theStringList += "\n" + theList[i];
}
MessageBox.Show(String.Format("{0}"), theStringList);
//combobox2.Items.AddRange(client.GetList("dairy"));
}
}
else
combobox2.Enabled = false;
}
使用==不等于。 – Joe 2012-03-19 01:10:03
编辑我的答案,以适应您的编辑 – 2012-03-19 11:37:43