1
嗨,我有一个对象列表,我需要找出我的ID是否已经在列表中。在对象类中,我设置了id,我只想知道在Ui中输入的是否已经被使用。检查对象列表是否包含属性
的Class
class Product
{
private string name = "";
private int id = 0;
private decimal Pvalue = 0.00m;
private static int lastId = 1;
public Product(string namep, decimal valuep)
{
this.name = namep;
this.id = lastId++;
this.Pvalue = valuep;
}
public override string ToString()
{
return name + " " + id + " "+ Pvalue;
}
public bool Equals(Product p)
{
return (p.id == this.id);
}
}
我试图去解决它:
int id;
bool test = int.TryParse(textBoxId.Text, out id);
if(test)
{
if(Inv.Contains(id))
{
label2.Text = "you already have this id";
}else
{
label2.Text = "you can use this id";
}
}
如果任何人有这是为什么不工作一个想法或一个更好的方式,它会拯救我的背面感谢您。
感谢您的帮助,但IM仍然得到同样的事情“对象不包含对ID的定义” – 1ftw1 2012-08-07 05:01:24
是什么类型'Inv'? – Dan 2012-08-07 05:02:14
它是不是对象? – 1ftw1 2012-08-07 05:03:21