2015-04-26 22 views
1

我试图计算一些属性,但我不知道它是如何如何。 proporties是string类型,我知道string值无法计算。所以我虽然关于使他们int。将字符串转换为本地int来计算

public class ItemProperties 
      { 
       public string Item { get; set; } 
       public string Description { get; set; } 
       public string Quantity { get; set; } 
       public string UnitPrice { get; set; } 
       public string Tax { get; set; } 

       public int TotalPay { get { return Quantity * UnitPrice; } set; } 
       //Now I get error on the line above because string can't calculate 

      } 

操作员“*”不能应用于类型“字符串”和 “串”

的操作数所以基本上我需要在stringQuantityUnitPrice当地int变量转换和然后用它们来计算。谁能帮我吗?

UPDATE:

下面是属性分配给string[]

var lines = File.ReadAllLines(openFileDialog.FileName); 
for (var i = 9; i + 2 < lines.Length; i += 6) 
      { 
       Items.Add(new ItemProperties { 
        Item = lines[i], 
        Description = lines[i + 1], 
        Quantity = lines[i + 2], 
        UnitPrice = lines[i + 3], 
        Tax = lines[i + 4] 
       }); 
      } 
      itemlst.ItemsSource = Items; 
+1

你为什么不能做数量和单价整数? public int Quantity {get;组; } public int UnitPrice {get;组; } – nimeshjm

+0

因为我将它们分配给'array of strings' varibales –

+0

为什么在将它们添加到数组之前将它们转换为字符串? –

回答

3

的快捷方式(没有错误检查)。将代码:

public int TotalPay { get { return Convert.ToInt32(Quantity) * Convert.ToInt32(UnitPrice); } } 

更多的位健壮的版本:

public int TotalPay 
    { 
     get 
     { 
      int qty; 
      int price; 

      if (int.TryParse(Quantity, out qty) && int.TryParse(UnitPrice, out price)) 
      { 
       return qty * price; 
      } 

      return 0; 
     } 
    } 

这可以解决您当前的问题,但如果属性保存int数据,则应将其定义为int。

将它们分配给字符串数组的代码应该将int属性转换为字符串,而不是相反。

编辑 这会将从文件读取的字符串属性转换为int。请注意,如果文件中的值不是整数,这将导致错误。你应该有一些错误处理来迎合这一点。

public class ItemProperties 
{ 
    public string Item { get; set; } 
    public string Description { get; set; } 
    public int Quantity { get; set; } 
    public int UnitPrice { get; set; } 
    public string Tax { get; set; } 

    public int TotalPay 
    { 
     get 
     { 
      return Quantity * UnitPrice; 
     } 
    } 
} 

    var lines = File.ReadAllLines(openFileDialog.FileName); 

    for (var i = 9; i + 2 < lines.Length; i += 6) 
     { 
      Items.Add(new ItemProperties 
      { 
       Item = lines[i], 
       Description = lines[i + 1], 
       Quantity = Convert.ToInt32(lines[i + 2]), 
       UnitPrice = Convert.ToInt32(lines[i + 3]), 
       Tax = lines[i + 4] 
      }); 
     } 
     itemlst.ItemsSource = Items; 
+0

谢谢。我在那里更新了我的问题,显示了字符串的赋值。你能帮忙把int属性转换成字符串吗? –

+0

为什么'Tax'仍然是'字符串','UnitPrice'是'int'?你会认为它们是'双单价'(或“浮动单价”)和“双重税”(或“浮动税”)。 –

+0

是的,人们可以这样认为。然而,这些都是特定于OP域的业务规则,他必须改变它们。我只是提出他需要做出的改变来回答OP的问题。 – nimeshjm

0

如果它们表示整数数据,则应该使上述属性为整数。 您可以使用静态Convert类解析字符串为整数,例如:

public class ItemProperties 
{ 
    public string Item { get; set; } 
    public string Description { get; set; } 
    public int Quantity { get; set; } 
    public int UnitPrice { get; set; } 
    public int Tax { get; set; } 

    public int TotalPay { get { return Quantity * UnitPrice; } set; } 

} 

使用:

ItemProperties prop; 
try 
{ 
    prop.Quantity = Convert.ToInt32(stringVar); 
} 
catch(FormatException) 
{ 
    // error handling 
} 
+0

静态在哪里? –

+0

'System.Convert'类是静态的:https://msdn.microsoft.com/en-us/library/system.convert%28v=vs.110%29.aspx – mcserep

+0

哦,我的意思是在你的代码中。我虽然假设你在你提供的代码示例中包含静态代码。我的错 –