简体   繁体   English

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

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

I was wondering if you cloud help me? 我想知道你的云对我有帮助吗?

I have an array that consist of items and prices and qty. 我有一个由项目,价格和数量组成的数组。 If the item exist it the array it must update the price and the qty, If it doesn't exits it must be added 如果该项目存在,它必须更新价格和数量,如果不存在,则必须添加它

Here is my code that i have tried: 这是我尝试过的代码:

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;
}

it is not working could you maby assist me on this? 它不起作用,您可以帮我做一下吗?

Arrays in C# cannot have entries added to them after being initialised. C#中的数组在初始化后无法向其添加条目。 You're better off using a List<String> instead, where you can add and remove entries from the list. 最好改用List<String> ,在这里您可以在列表中添加和删除条目。 Alternatively consider a Dictionary<Int32, String> , which would let you use the ItemCode as an identifier to make finding a given entry easier. 或者,考虑使用Dictionary<Int32, String> ,它可以让您将ItemCode用作标识符,以使查找给定条目变得更加容易。

As a furthur point, instead of storing all your item data in a delimited string, make a new Class for them, with the various details as properties, and then you can use a theoretical Dictionary<Int32, ItemObject> for better clarity. 更重要的是,不必将所有项目数据存储在定界字符串中,而是为它们创建一个具有各种详细信息作为属性的新Class,然后可以使用理论性Dictionary<Int32, ItemObject>以获得更好的清晰度。

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

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