C# 将字符串转换为本地int以计算

C# 将字符串转换为本地int以计算,c#,converter,C#,Converter,我试图计算一些属性,但我不确定如何计算。 比例是string类型,我知道string值无法计算。所以我想把它们变成int public class ItemProperties { public string Item { get; set; } public string Description { get; set; } public string Quantity

我试图计算一些属性,但我不确定如何计算。 比例是
string
类型,我知道
string
值无法计算。所以我想把它们变成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”类型和 “字符串”

因此,基本上我需要将
字符串
数量
单价
转换为本地
int
变量,然后使用它们进行计算。有人能帮我吗

更新:

下面是将属性分配给
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;
var lines=File.ReadAllLines(openFileDialog.FileName);
对于(变量i=9;i+2
快速方法(无错误检查)是:

    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属性转换为字符串,而不是反过来

编辑 这将把从文件中读取的字符串属性转换为整数。请注意,如果文件中的值不是整数,这将导致错误。您应该有一些错误处理来满足这一需求

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;
公共类ItemProperties
{
公共字符串项{get;set;}
公共字符串说明{get;set;}
公共整数数量{get;set;}
公共整数单价{get;set;}
公共字符串Tax{get;set;}
公共收入总额
{
得到
{
退货数量*单价;
}
}
}
var lines=File.ReadAllLines(openFileDialog.FileName);
对于(变量i=9;i+2
如果上述属性表示整数数据,则应将其设置为整数。 您可以使用static
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
}

为什么你们不能做数量和单价的整数?public int Quantity{get;set;}public int UnitPrice{get;set;}因为我正在将它们分配给字符串数组
varible为什么不在将它们添加到数组之前将它们转换为字符串?@RufusL我不知道如何做。你能帮我一下吗?不,我是说
public int-Quantity
,然后
Quantity=Convert.ToInt32(行[I+2]),
谢谢。我在那里更新了我的问题,并将赋值显示到字符串中。在完成分配之前,您是否可以帮助将整数属性转换为字符串为什么
Tax
仍然是
string
,而
UnitPrice
仍然是
int
?你会认为它们是双重单价(或浮动单价)和双重税(或浮动税)。是的,可以这样假设。然而,这些是OP的领域特有的业务规则,他必须更改它们。我只是建议他需要做些改变来回答OP的问题。
System.Convert
类是静态的:哦,我的意思是在你的代码中。我认为您应该在给出的代码示例中包含static。我的错