1

こんにちは、私はオブジェクトのリストを持っています。私が持っている ID が既にリストにあるかどうかを調べる必要があります。オブジェクト クラスで ID を設定しました。Ui に入力された ID が既に使用されているかどうかを確認したいだけです。

クラス

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";            
                }

            }

なぜこれが機能しないのか、またはより良い方法について誰かが考えを持っているなら、それは私の裏側を救うでしょうありがとう.

4

1 に答える 1

5

に変更private int id = 0;public int Id { get; set; }ます。また、すべての参照を からidに変更しIdます。

using System.Linqビジネス ロジック ファイルに を追加します。

if (Inv.Contains(id))に変更if (Inv.Any(x => x.Id == id))

于 2012-08-07T04:57:21.663 に答える