【问题标题】:Append 'Called by reference variables' to a text file将“引用变量调用”附加到文本文件
【发布时间】:2015-05-20 09:15:55
【问题描述】:

我正在 Selenium Web 驱动程序中编写测试用例,我想将结果输出到日志文件。测试用例应在文本文件中按每行编号。编号已经实现,但附加测试用例是我无法实现的。问题是我想附加通过引用传递的变量组合的 for 循环。这些变量将是测试用例。如果需要更多解释,请告诉我。

问题在于连接定义的变量只会为所有变量附加​​一个值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication5
{
    class Class1
    {
        public static int counter;
        public static String[] test = {"1", "2" };
        public static String[] test1 = { "a", "b" };

        public static void Main(String[] args)
        {
            new Class1().testing2();
        }

        public String testing(String ab, String cd)
        {
            //Count the number of times the method is called
            ++counter;
            //Write it to a text file
            File.WriteAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt", counter.ToString());
            //Read from that text file
            String Readfiles = File.ReadAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt");
            //Convert to integer
            Int32 myInt = Int32.Parse(Readfiles);
            //Store in array variable
            String[] start = new String[myInt];
            //iterate through
            for (int i = 0; i < myInt; ++i)
            {
                **start[i] = (i + 1).ToString() +ab +cd;**
            }
            //Write into another text file on new lines
            File.WriteAllLines(@"C:\Users\ken4ward\Desktop\Tidy\Writing.txt", start);

            String gb = ab; String th = cd; String you = ab + cd;
            return you;   
        }

        public void testing2()
        { 
            foreach(var item in Class1.test)
            {
                foreach (var item1 in Class1.test1)
                {
                    String test = testing(item, item1);
                    Console.WriteLine(test);
                    Console.ReadLine();
                }
            }
        }
    }
}

当我运行它时,这是文本文件中的输出:

1 2b
2 2b
3 2b
4 2b

我想在文本文件中输出的是:

1 1a
2 1b
3 2a
4 2b

【问题讨论】:

  • 您的问题不清楚。您能否展示该方法的输出以及您真正希望它输出的内容?
  • 谢谢。我已经编辑它添加了我期望的输出。
  • 这条线是什么意思 **start[i] = (i + 1).ToString() +ab +cd;** ?你是说start[i] = (i + 1).ToString() +ab +cd; 吗?
  • 这正是问题所在。这意味着将通过引用传递的变量附加到每个编号的行。我想要的是,对于每次迭代,将调用的变量的串联添加到每一行。

标签: c# arrays file for-loop foreach


【解决方案1】:

添加一个全局变量

public static String[] lines = { };

testing() 更改为:

    public String testing(String ab, String cd)
    {
        //Count the number of times the method is called
        ++counter;
        //Write it to a text file
        File.WriteAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt", counter.ToString());
        //Read from that text file
        String Readfiles = File.ReadAllText(@"C:\Users\ken4ward\Desktop\Tidy\WriteLines.txt");
        //Convert to integer
        Int32 myInt = Int32.Parse(Readfiles);
        //Store in array variable
        String start = myInt.ToString() + ab + cd;
        //Store the new data in the array
        Array.Resize(ref lines, lines.Length + 1);
        lines[lines.GetUpperBound(0)] = start;
        //Write into another text file on new lines
        File.WriteAllLines(@"C:\Users\ken4ward\Desktop\Tidy\Writing.txt", lines);
        //String gb = ab; String th = cd;
        String you = ab + cd;

        return you;   
    }

因为WriteAllText意味着覆盖文件。

for (int i = 0; i < myInt; ++i)
{
    start[i] = (i + 1).ToString() +ab +cd;
}

(ab + cd) 上面的代码等于2b,这意味着您只是更改了行号,2b 保持不变。

【讨论】:

  • 更好的方法是附加输出文件。但这会有点复杂。
  • 我要创建一个变量“行”吗?这会引发一个错误,它在当前上下文中不存在。欣赏。如果是,什么数据类型?
  • 添加一个全局变量lines。请参阅我的答案的第一行。
  • 下方String[] test1 = { "a", "b" };
  • 谢谢。言语不足以欣赏。问题解决了!
猜你喜欢
  • 1970-01-01
  • 2014-03-17
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多