0

プロパティを宣言しています - 開発環境で作業しているときは正常に動作しますが、ステージング環境にアクセスすると常に null が返されます。なぜこうなった?コードは両方の環境で変更されることはありません。これがコードです。

private ProductCollection _productCollection;    

public ProductCollection ProdCollection
{
    get
    {
        _productCollection = MWProductReviewHelper.GetDistinctProductFromTill(StoreId, TDPId, ReceiptId);
        if (_productCollection.Count > 0)
            return _productCollection;
        else
            return null;
    }
}

private ProductCollection _guaranteedProductCollection = new ProductCollection();

public ProductCollection GuaranteedProductCollections
{
    get
    {
        if (_guaranteedProductCollection.Count > 0)
        {
            return _guaranteedProductCollection;
        }
        else
        {
            return _guaranteedProductCollection = MWProductGuaranteedHelper.CheckGuaranteedProductsFromList(ProdCollection);  // the problem appears to be here... 
        }
    }
}

アクセスはこんな感じです。

if (GuaranteedProductCollections.Count > 0)
{
    ProductCollection _prodCollection = GuaranteedProductCollections; // return null
}

その中には常に 1 つの製品があります。これは、ブレークポイントを設定するとわかります。

4

2 に答える 2

2

そのセットを 2 行に重ねてみましたか? セットが完成する前に戻ってくる可能性があるようです。

public ProductCollection GuaranteedProductCollections
      {
        get
          {
            if (_guaranteedProductCollection.Count > 0)
              {
                 return _guaranteedProductCollection;
              }
            else
              {
                _guaranteedProductCollection = MWProductGuaranteedHelper.CheckGuaranteedProductsFromList(ProdCollection);  // the problem appears to be here... 
                return _guaranteedProductCollection;
              }
           }
      }
于 2012-08-16T09:21:29.637 に答える
1

プロパティがnullを返している場合は、次のGuaranteedProductCollectionsいずれかの理由である必要があります。

  • MWProductGuaranteedHelper.CheckGuaranteedProductsFromList(ProdCollection)nullを返しています。
  • 他の何か_guaranteedProductCollectionがnullに設定されています(これは_guaranteedProductCollection例外をスローするため、カウントをテストするときにnullではない可能性があります)

余談ですが、通常、この方法でコレクションをnull初期化する場合は、初期化されていないコレクションを表すために使用するのが最善です。これにより、コレクションが初期化されますが、空になります。私はあなたのプロパティを次のように実装します:

public ProductCollection GuaranteedProductCollections
{
    get
    {
        if (_guaranteedProductCollection == null)
        {
            _guaranteedProductCollection = WProductGuaranteedHelper.CheckGuaranteedProductsFromList(ProdCollection);
        }
        return _guaranteedProductCollection;
    }
}

これは、 null合体(??)演算子を使用して1行に簡略化できます。

public ProductCollection GuaranteedProductCollections
{
    get
    {
        return _guaranteedProductCollection
            ?? _guaranteedProductCollection = WProductGuaranteedHelper.CheckGuaranteedProductsFromList(ProdCollection);
    }
}
于 2012-08-16T09:44:17.530 に答える