C# 将从文本文件加载到 List<> 中的数组值相乘
C# Multiplying the values of an array together which are loaded from a textfile into a List<>
所以我有一个数组 strArray,它存储我的文本文件的值,它有 3 列。我认为这称为二维或三维数组,不确定。或者也许是一维的。我有一个名为 Inventory 的 List<>,它向其中添加了数据。
我目前有三个成功的专栏我只需要第四个。第四列是第二列和第三列相乘,就是一个总价。第二列是整数,"Number of Items",第三列是小数,"Price",第四列是小数,"Total Price" 即商品数量 * 价格。
我将继续 post 我的代码,我还使用四个列表框作为数据 。三列(或三个列表框)工作正常,但我只需要弄清楚第四个。
抱歉,代码量太大,我想如果我复制了所有代码,就可以更轻松地查看之前是否发生错误。 btnLoadInfo_Click 是主要问题所在的 event/method。
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;
}
您正在尝试将两个字符串相乘。相反,乘以您已经解析的数值:
currentItem.TotalPrice = currentItem.NumberOfItems * currentItem.Price;
您的 TotalPrice 属性 应该是一个数学方程式,而不是您独立于商品数量及其价格设置的东西。
将 属性 更改为:
public decimal TotalPrice{
get{ return NumberOfItems * Price; }
}
删除循环中设置 TotalPrice 的行;不再需要,因为您已经设置了商品价格和商品数量;总价内在地遵循这些
所以我有一个数组 strArray,它存储我的文本文件的值,它有 3 列。我认为这称为二维或三维数组,不确定。或者也许是一维的。我有一个名为 Inventory 的 List<>,它向其中添加了数据。
我目前有三个成功的专栏我只需要第四个。第四列是第二列和第三列相乘,就是一个总价。第二列是整数,"Number of Items",第三列是小数,"Price",第四列是小数,"Total Price" 即商品数量 * 价格。
我将继续 post 我的代码,我还使用四个列表框作为数据 。三列(或三个列表框)工作正常,但我只需要弄清楚第四个。
抱歉,代码量太大,我想如果我复制了所有代码,就可以更轻松地查看之前是否发生错误。 btnLoadInfo_Click 是主要问题所在的 event/method。
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;
}
您正在尝试将两个字符串相乘。相反,乘以您已经解析的数值:
currentItem.TotalPrice = currentItem.NumberOfItems * currentItem.Price;
您的 TotalPrice 属性 应该是一个数学方程式,而不是您独立于商品数量及其价格设置的东西。
将 属性 更改为:
public decimal TotalPrice{
get{ return NumberOfItems * Price; }
}
删除循环中设置 TotalPrice 的行;不再需要,因为您已经设置了商品价格和商品数量;总价内在地遵循这些