2016-06-01 53 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'确实带有代码 –