如何将 StringBuilder 初始化为 null

发布于 2024-11-27 05:41:49 字数 576 浏览 0 评论 0原文

大家好,我遇到了 StringBuilder 的问题。在自动化过程中,我会将 testfailed 的值分配给 StringBuilder,因为

private StringBuilder testFailed = new StringBuilder();

public void Test1()
{
    testFailed = SomeTest();
}

public void Test2()
{
    testFailed = null;

    //testFailed = testFailed.Clear();
    //testFailed = new StringBuilder();

    testFailed = someTest1();
}

当第一个测试失败时,testFailed StringBuilder 将附加下一个测试结果或者如果测试通过,则 testFailed 字符串将显示 testFailed b 结果值。当我使用注释方法并尝试时,它对此不起作用。让我知道任何其他方法。提前致谢。

Hi all I am facing a problem with StringBuilder. While automating, I will assign the value of testfailed to a StringBuilder as

private StringBuilder testFailed = new StringBuilder();

public void Test1()
{
    testFailed = SomeTest();
}

public void Test2()
{
    testFailed = null;

    //testFailed = testFailed.Clear();
    //testFailed = new StringBuilder();

    testFailed = someTest1();
}

When ever first test fails, the testFailed StringBuilder will append the next test result or if test passes then testFailed string will display the testFailed b result value. As I used commented methods and tried it didn't work for that. Let me know any other ways. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断爱 2024-12-04 05:41:49

我不确定你想从中实现什么目标。但我猜您想汇总您正在运行的各种测试的错误。如果是这种情况,您不需要重新分配 StringBuilder 类,只需使用单个实例并对其使用 Appends ,您的测试将返回字符串作为结果。

例如。

private StringBuilder testFailed;

public void Test1()
{
    testFailed = new StringBuilder();
    testFailed.AppendLine(SomeTest());
}

public void Test2()
{ 
    testFailed = new StringBuilder();
    testFailed.AppendLine(someTest1());
}

我不知道你为什么采取这种方法,但每个人都有自己的方法。我还建议您看一下 NUnit。

I am not sure what you are trying to achieve from this. But I am guessing you want to aggregate the errors from various tests that you are running. If thats the case you dont need to reassign the StringBuilder class just use a single instance and use Appends on it and your test will return string as a result.

Eg.

private StringBuilder testFailed;

public void Test1()
{
    testFailed = new StringBuilder();
    testFailed.AppendLine(SomeTest());
}

public void Test2()
{ 
    testFailed = new StringBuilder();
    testFailed.AppendLine(someTest1());
}

I dont know why you are taking this approach, but to each his own. I would also suggest you take a look at NUnit.

(り薆情海 2024-12-04 05:41:49

请不要使用 null 作为值 - 这是一个坏主意。我会使用一个字符串列表来完成您正在做的事情。

Please don't use null as a value - it's a bad idea. I'd use a list of strings for what you are doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文