【问题标题】:Use a multidimensional array of strings to write to file使用多维字符串数组写入文件
【发布时间】:2021-04-09 12:58:15
【问题描述】:

我正在尝试开发一个简单的 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# 和编程新手,非常感谢任何帮助。

【问题讨论】:

    标签: c# arrays multidimensional-array file-io epos


    【解决方案1】:

    您正在循环浏览 ProductList:

    string[] Productlist = { Products[0], Sizes[0] };

    这里面只有 Product 的第一个索引和 Sizes 的第一个索引。所以它只会输出两个值。

    您需要创建两个嵌套的for 循环,循环通过ProductsSizes

    【讨论】:

    • 您好,感谢您的回答。它不会让我在各自的括号内留下没有值的产品或尺寸,所以我看不到让它将所有索引项写入文件的方法。
    • 我不确定你想要达到什么目的。你能澄清一下吗?
    • 道歉。所以我有两个数组,Product 和 Size,我希望能够将它们链接在一起,以便在程序运行时将它们写入文件以显示我的期初库存是多少。每个产品有 13 种产品和 5 种尺寸。当我打开文本文件时,我希望它显示产品,然后显示它的可用尺寸。例如,“1989 Jersey, XS,S,M,L,XL” 这有意义吗?
    • 基本上,我只想要我声明的二维数组将其所有内容写入文件,如果这有意义吗?再次感谢。
    • 数组不是做到这一点的最佳方式,但这里有一种方法可以做我认为你正在尝试做的事情:Foreach 循环最好在这里使用,因为它不会超出数组,它需要很少的工作。 foreach (var product in Products) { outputfile.WriteLine(product); foreach(var size in Sizes) { outputfile.WriteLine(size); } }
    【解决方案2】:

    如果您只是想列出产品,您可以这样做

           try
            {
                //enable StreamwWriter Object
                StreamWriter outputfile;
    
                // create file
                outputfile = File.CreateText("ProductList.txt");
    
                //writing array contents to file
                for (int index = 0; index < Products.Length; index++)
                {
                    outputfile.WriteLine(Products[index]);
                    outputfile.WriteLine("");
                }
    
                outputfile.Close();
    
                MessageBox.Show("Data Entered");
            }
    
            catch
            {
                MessageBox.Show("Error");
            }
    

    一般来说,我会创建一个类来保存您的产品。类似的东西

        public class Product
        {
            public string ProductName { get; set; }
            
            public List<string> Sizes { get; set; }
            
            public decimal Price { get; set; }
        }
    

    那么你可以做这样的事情来转换成新的类

          Productlist = new List<Product>();
            for (int x = 0; x < Products.Length; x++)
            {
                Productlist.Add(new Product()
                {
                    ProductName = Products[x],
                    Price = Price[x],
                    Sizes = Sizes.ToList()
                });
            }
           WriteToFileOpeningStock();
    

    然后您可以进行以下更改以在文件中列出产品

      private void WriteToFileOpeningStock()
        {
            try
            {
                //enable StreamwWriter Object
                StreamWriter outputfile;
    
                // create file
                outputfile = File.CreateText("ProductList.txt");
    
                //writing array contents to file
                foreach(Product product in Productlist)
                {
                    outputfile.WriteLine(product.ProductName);
                    outputfile.WriteLine("");
                }
    
                outputfile.Close();
    
                MessageBox.Show("Data Entered");
            }
    
            catch
            {
                MessageBox.Show("Error");
            }
            
        }
    

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多