2016-06-01 50 views
0

我有一個文本框,其值是自然正數,因爲我做了一些計算後,從另一個文本框中生成它,我的文本框保存的結果肯定是數字,當我嘗試將該值轉換爲它始終給出0的整數,而文本框的值保持大於0且根本不爲空。
我都嘗試轉換方法在這裏msdn
我也試過TryParse方法在這裏成立Convert textbox text to integer
我創建2個破發點看到轉換後的文字都文本框中的文本和整數值的結果
這裏是我的代碼:將文本框文本轉換爲整數給出0

MessageBox.Show(updateProductTQ.Text, "Quantity"); 
p.ProductQuantity = int.Parse(updateProductTQ.Text); 
MessageBox.Show(p.ProductQuantity.ToString(), "Quantity"); 

updateProductTQ是我的文本框的名字,p.ProductQuantity是整數
第一MessageBox輸出是在文本框中顯示的數字10現在
第二個MessageBox輸出很大0

有人能告訴我爲什麼我得到0嗎?
我嘗試另一個文本框持有數中的轉換和正常工作是很奇怪的事情

這裏是我的ProductQuantity定義:

public int ProductQuantity 
    { 
     get 
     { 
      if (ProductCartoons >= 0) 
      { 
       if (ProductBigBox > 0 && ProductSmallBox > 0 && ProductMedBox > 0 && ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductBigBox * ProductMedBox * ProductSmallBox * ProductItems; 
       } 
       else 
       if (ProductBigBox > 0 && ProductMedBox > 0 && ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductBigBox * ProductMedBox * ProductItems; 
       } 
       else 
       if (ProductBigBox>0 && ProductSmallBox > 0 && ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductBigBox * ProductSmallBox * ProductItems; 
       } 
       else 
       if (ProductMedBox > 0 && ProductSmallBox > 0 && ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductMedBox * ProductSmallBox * ProductItems; 
       } 
       else 
       if (ProductBigBox>0 && ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductBigBox * ProductItems; 
       } 
       else 
       if (ProductMedBox > 0 && ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductMedBox * ProductItems; 
       } 
       else 
       if (ProductSmallBox > 0 && ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductSmallBox * ProductItems; 
       } 
       else 
       if (ProductItems >= 0) 
       { 
        _ProdQuan = ProductCartoons * ProductItems; 
       } 
      } 
      return _ProdQuan; 
     } 
     set { _ProdQuan = value; } 
    } 

,並通過這些文本框提出的所有通過結合和完成它運作良好:

public int ProductCartoons 
    { 
     get { return _Cartoons; } 
     set 
     { 
      _Cartoons = value; 
      RaisePropertyChangedEvent("ProductQuantity"); 
     } 
    } 

    public int ProductBigBox 
    { 
     get { return _BigBox; } 
     set 
     { 
      _BigBox = value; 
      RaisePropertyChangedEvent("ProductQuantity"); 
     } 
    } 

    public int ProductMedBox 
    { 
     get { return _MedBox; } 
     set 
     { 
      _MedBox = value; 
      RaisePropertyChangedEvent("ProductQuantity"); 
     } 
    } 

    public int ProductSmallBox 
    { 
     get { return _SmallBox; } 
     set 
     { 
      _SmallBox = value; 
      RaisePropertyChangedEvent("ProductQuantity"); 
     } 
    } 

    public int ProductItems 
    { 
     get { return _Items; } 
     set 
     { 
      _Items = value; 
      RaisePropertyChangedEvent("ProductQuantity"); 
     } 
    } 
+0

你可以發佈'updateProductTQ.Text'的值嗎? –

+2

你能告訴我們'ProductQuantity'屬性是如何定義的嗎? –

+0

您測試過的產品是否爲ProductCarton> 0?然後它不會直接返回存儲的'_ProdQ​​uan',而是重新計算它。 –

回答

0

在那裏,你看到的原因,如果你注意到ProductQuantity定義它的值(吸氣)取決於ProductCartoons,如果該值是0你總是得到0ProductQuantity

我會建議使用調試器,並通過一步來看看在這種情況下導致0是什麼。

+0

如果'ProductCartoons'爲'0''' Product'''''''''''''知道,但它們不是0' –

+0

您的意思是說'ProductCartoons'不是_zero_? –

+0

是'ProductCartoons'不是'0'確實帶有代碼 –