【问题标题】:C# Multiplying the values of an array together which are loaded from a textfile into a List<>C# 将从文本文件加载到 List<> 中的数组的值相乘
【发布时间】:2017-05-11 04:07:36
【问题描述】:

所以我有一个数组 strArray,它存储我的文本文件的值,它有 3 列。我认为这称为二维或三维数组,不确定。或者也许是一维的。我有一个名为 Inventory 的 List,它将数据添加到其中。

我目前有三个成功的专栏,我只需要第四个。第四列是第二列和第三列相乘,就是总价。第二列是整数,“项目数量”,第三列是小数,“价格”,第四列是小数,“总价格”,即项目数量 * 价格。

我将继续发布我的代码,我还使用四个列表框来存储数据。三列(或三个列表框)可以正常工作,但我只需要弄清楚第四列。

抱歉,代码量很大,我想如果我将所有代码都复制了,就可以更容易地查看之前是否发生了错误。 btnLoadInfo_Click 是主要问题所在的事件/方法。

namespace TCSCapstone
{
public partial class frmInventory : Form
{
    List<frmInventory> Inventory = new List<frmInventory>();

    public frmInventory()
    {
        InitializeComponent();
    }

    public string ItemName { get; set; }
    public int NumberOfItems { get; set; }
    public decimal Price { get; set; }
    public decimal TotalPrice { get; set; }

    string selectedList = "";

    private void cmbList_SelectedIndexChanged(object sender, EventArgs e)
    {
        selectedList = this.cmbList.GetItemText(this.cmbList.SelectedItem);

        lstItemName.DataSource = null;
        lstNumberOfItems.DataSource = null;
        lstPrice.DataSource = null;

        lstItemName.Items.Clear();
        lstNumberOfItems.Items.Clear();
        lstPrice.Items.Clear();
        lstTotalPrices.Items.Clear();

        if (selectedList == "Creative Construction")//if the selected combo box item equals the exact string selected
        {
            selectedList = "creative"; //then the string equals creative, which is creative.txt but I add the .txt in the btnLoadInfo method
        } else if (selectedList == "Paradise Building")
        {
            selectedList = "paradise";//this is for paradise.txt
        }
        else if (selectedList == "Sitler Construction")
        {
            selectedList = "sitler";//this is for sitler.txt
        }
        else
        {
            MessageBox.Show("Please select one of the items.");
        }
    }`



        private void btnLoadInfo_Click(object sender, EventArgs e)
    {
        Inventory.Clear(); //Clears the entire Inventory List

         using (StreamReader invReader = new StreamReader(selectedList + 
 ".txt"))
         {
             while (invReader.Peek() >= 0)
             {
                 string str;
                 string[] strArray;
                 str = invReader.ReadLine();

                 strArray = str.Split(',');
                 frmInventory currentItem = new frmInventory();

                 currentItem.ItemName = strArray[0];
                 currentItem.NumberOfItems = int.Parse(strArray[1]);
                 currentItem.Price = 
 decimal.Parse(strArray[2]);

                 strArray[1].
                 currentItem.TotalPrice = decimal.Parse(strArray[1] * 
strArray[2]);

                 Inventory.Add(currentItem);

             }
         }


         displayLists(); //Calls the displayLists method to update list 
//boxes at the end of the button click event
     }//end of btnLoadInfo

     void displayLists()
     {
         //Resets the listboxes datasources by setting them to null
         lstItemName.DataSource = null;
         lstNumberOfItems.DataSource = null;
         lstPrice.DataSource = null;

         lstItemName.Items.Clear();
         lstNumberOfItems.Items.Clear();
         lstPrice.Items.Clear();
         lstTotalPrices.Items.Clear();

         lstItemName.DisplayMember = "ItemName";
         lstItemName.ValueMember = "";
         lstItemName.DataSource = Inventory;

         lstNumberOfItems.DisplayMember = "NumberOfItems";
         lstNumberOfItems.ValueMember = "";
         lstNumberOfItems.DataSource = Inventory;

         lstPrice.DisplayMember = "Price";
         lstPrice.ValueMember = "";
         lstPrice.DataSource = Inventory;
     }

【问题讨论】:

  • 除了随机的strArray[1]. 坐在那里,真正的问题是什么?
  • 另外,您已经将 strArray[1] 和 2 的值分别拉入 int 和 decimal,因此尝试再次解析它们没有多大意义(也没有尝试将两个相乘)串在一起)

标签: c# arrays filereader streamreader


【解决方案1】:

您的 TotalPrice 属性应该是一个数学方程式,而不是您独立于商品数量及其价格设置的东西。

将属性更改为:

public decimal TotalPrice{
  get{ return NumberOfItems * Price; }
}

删除循环中设置 TotalPrice 的行;不再需要,因为您已经设置了商品价格和商品数量;总价自然地来自这些

【讨论】:

    【解决方案2】:

    您正在尝试将两个字符串相乘。相反,将已经解析的数值相乘:

    currentItem.TotalPrice = currentItem.NumberOfItems * currentItem.Price;
    

    【讨论】:

    • 非常感谢,这正是我需要的解决方案。我想多了,我有我需要的所有数据。再次感谢:)
    • 实际上你需要做的是让你的 TotalPrice 属性成为一个 get (只读)来做乘法。现在,您有一个对象,可以让您以 79.99 或 7.62 的总价格拥有 10 个 5 美元的物品。或其他任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多