我试图计算一些属性,但我不知道它是如何如何。 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
}
操作员“*”不能应用于类型“字符串”和 “串”
的操作数所以基本上我需要在string
Quantity
和UnitPrice
当地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;
你为什么不能做数量和单价整数? public int Quantity {get;组; } public int UnitPrice {get;组; } – nimeshjm
因为我将它们分配给'array of strings' varibales –
为什么在将它们添加到数组之前将它们转换为字符串? –