我似乎无法看到我的错误在这里。我知道这不是最好的方法,但我有义务使用Arraylist。C#arraylist搜索对象属性问题
无论如何,我知道我的arraylist包含正确的元素,我想知道我的Item类的属性Number是否与'searchNr'相同。
如果是,我想用ToString()打印出该元素。如果我搜索“107”(Arraylist中最后一个元素的Number属性,我确实找到它了),但是找不到任何其他数字属性。
public void Search(object sender, EventArgs e)
{
string searchNrString = searchTextBox.Text;
int searchNr = Int32.Parse(searchNrString);
foreach (Item product in item)
{
if (product.Number == searchNr) //if we find the number
{
resultTextBox.Text = ((Item)product).ToString();
}
else if (product.Number != searchNr) //if we don't find the number
{
string debug;
debug = string.Format("Could not find the number {0} arraylist :{1}", product.Number, item.Count); //debug här!
resultTextBox.Text = debug;
}
}
}
ArrayList在哪里?另外,你使用的是什么版本的.NET? –