我正在尝试开发一个简单的 EPOS 系统来记录库存和处理交易。目前我希望它使用 Streamwriter 将我的数组(产品、价格、大小、库存水平)的内容以及顶部的随机日期写入文件,但我无法破解它。我为这些数组声明的全局变量如下。
readonly static string[] Products = { "1989 Jersey", "1977 Jersey", "2001 Jersey", "1986 Jersey", "1990 Jersey", "2005 Jersey", "1992 Jersey", "1996 Jersey", "1985 Jersey", "1989 Jersey", "1991 Jersey", "1992 Jersey", "1986 Jersey"};
readonly static decimal[] Price = {26m, 24m, 21m, 25m, 19m, 22m, 23m, 18m, 16.50m, 17.50m, 14m, 20m, 17.50m};
readonly static string[] Sizes = {"Extra Small", "Small", "Medium", "Large", "Extra Large"};
readonly static int[,] StartingStock = {{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 }};
我目前正在尝试让我的代码正常工作。我在表单加载事件中调用上述方法,它只将索引 0,0 处的内容传递给文件。
private void WriteToFileOpeningStock()
{
string[] Productlist = { Products[0], Sizes[0] };
try
{
//enable StreamwWriter Object
StreamWriter outputfile;
// create file
outputfile = File.CreateText("ProductList.txt");
//writing array contents to file
for (int index = 0; index < Productlist.Length; index++)
{
outputfile.WriteLine(Productlist[index]);
outputfile.WriteLine("");
}
outputfile.Close();
MessageBox.Show("Data Entered");
}
catch
{
MessageBox.Show("Error");
}
我试着写 string[] Productlist = { Products[0-12], Sizes[0-4] }; 让它传递所有数组内容,但它会引发异常“索引超出数组范围”。我是 C# 和编程的新手,所以非常感谢任何帮助。