繁体   English   中英

在数组C#中添加和更改项目

[英]Adding and Changing Items in a array C#

我想知道你的云对我有帮助吗?

我有一个由项目,价格和数量组成的数组。 如果该项目存在,它必须更新价格和数量,如果不存在,则必须添加它

这是我尝试过的代码:

if (line.Contains(ItemCode))
{
    string[] details = line.Split(new string[] { "|" }, StringSplitOptions.None);
    {
        for (int i = 0; i < details.Length; i++)
        {
            if (details[i].Contains(ItemCode))
            {
                string[] line_details = details[i].Split(',');
                string replace = line_details[2].Trim() + "," + line_details[3].Trim();
                double NewQty = double.Parse(Qty) + double.Parse(line_details[2]);
                double NewPrice = (double.Parse(UnitPrice) * double.Parse(Qty));
                double NewUnitPrice = NewPrice + double.Parse(line_details[3]);
                string new_replace = NewQty + "," + NewUnitPrice;
                line = line.Replace(replace, new_replace);
            }
        }
    }
}
else
{  
    line = line + "\"Detail\",0," + Qty + "," + (double.Parse(UnitPrice) * double.Parse(Qty)) + "," + InclusivePrice + ",\"" + UnitUsed + "\"," + TaxType + "," + DiscountType + "," + DiscountPercentage + ",\"" + ItemCode + "\",\"" + Description + "\"," + SearchType + "," + "\"\"" + ",\"" + MultiStore + "\"|" + Environment.NewLine;
}

它不起作用,您可以帮我做一下吗?

C#中的数组在初始化后无法向其添加条目。 最好改用List<String> ,在这里您可以在列表中添加和删除条目。 或者,考虑使用Dictionary<Int32, String> ,它可以让您将ItemCode用作标识符,以使查找给定条目变得更加容易。

更重要的是,不必将所有项目数据存储在定界字符串中,而是为它们创建一个具有各种详细信息作为属性的新Class,然后可以使用理论性Dictionary<Int32, ItemObject>以获得更好的清晰度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM