Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WinForms-代码在数组之前工作,但在数组之后放置时失败_C#_.net_Winforms - Fatal编程技术网

C#WinForms-代码在数组之前工作,但在数组之后放置时失败

C#WinForms-代码在数组之前工作,但在数组之后放置时失败,c#,.net,winforms,C#,.net,Winforms,我正在尝试使用以下代码将一段文本附加到RichTextBox Form2.codeOutput.Text+=“测试” 如果将其放置在我的阵列之前(如下所示),则效果良好: 但是,当我将其放置在阵列之后时,它不起作用: public static void newString(string curLine) { string[] parsedString = new string[3]; parsedString[0] = "

我正在尝试使用以下代码将一段文本附加到RichTextBox
Form2.codeOutput.Text+=“测试”
如果将其放置在我的阵列之前(如下所示),则效果良好:

但是,当我将其放置在阵列之后时,它不起作用:

public static void newString(string curLine)
        {
            string[] parsedString = new string[3];
            parsedString[0] = "string";
            parsedString[1] = curLine.Substring(7, 12);
            parsedString[2] = curLine.Substring(23, curLine.Length);

            Form2.codeOutput.Text += "test";
        }
我试图寻找解决办法,但没有得到任何好的结果;任何帮助都将不胜感激

编辑1:我应该补充一点,当代码不起作用时,它不会给出错误、崩溃或任何东西;文本根本不显示在richtextbox中。

编辑2:除了替换了
Form2.codeOutput.Text+=“test”之外,我尝试了相同的方法带有
Debug.WriteLine(“测试”),同一问题;当将代码放在数组之前而不是之后时,代码可以工作。

传递到方法中的“curLine”值是多少? 如果startIndex小于零或大于此实例的长度,则子字符串将抛出ArgumentOutOfRangeException

public static void Main()
{
    string value="abcdefghijklmnopqrstuvwxyz1234567890";
    newString(value);
}

public static void newString(string curLine)
{
     string[] parsedString = new string[3];
     parsedString[0] = "string";
     parsedString[1] = curLine.Substring(7, 12);
     Console.WriteLine(parsedString[1]);
     parsedString[2] = curLine.Substring(23); // Changes here
     Console.WriteLine(parsedString[2]);

     Console.WriteLine("Done");
     //Form2.codeOutput.Text += "test";
}
检查数组中的值

此方法可能在两种情况下产生ArgumentOutOfRangeException:

如果参数startIndex或length小于零。
如果startIndex+length表示某个位置不在当前实例中。

好,这很有趣。容易生气。第一件事:总是从文档开始

其次,子字符串的第二个参数是请求的子字符串的长度,而不是所需的最后一个索引。Do
curLine.Substring(23,curLine.Length-23)你应该很好。现在,您告诉子字符串从索引23开始,并给您下一个curLine.length字符。显然,这是失败的,因为它不是23个以上的卷曲长度字符

来自文档@_


curLine是“abcdefghijklmnopqrstuvwxyz1234567890”--它只是我用来测试东西的占位符值。
parsedString[2]=curLine.Substring(23,curLine.Length)
更改为
parsedString[2]=curLine.Substring(23,curLine.Length-1)您是否遇到任何异常?看起来这可能就是这里发生的事情。。在方法内部的第一行上放置一个断点并逐步遍历它,然后查看发生了什么subStringMethod出现了一些问题,您可以通过将Debug.WriteLine(“”)放在每个数组元素分配下面来对这个问题进行排序。调试此代码时发生了什么@psj01@BijuKalanjoor我发现了这个问题,前两个很好,但第三个给出了“System.ArgumentOutOfRangeException”,尝试添加curLine=“abcdefghijklmnopqrstuvwxyz1234567890”;是newString方法的第一条语句。如果这可以正常工作,下一步将改变curLine参数中的值。并快速查看每个子串statement@BijuKalanjoor我试过了,但是我得到了System.ArgumentOutOfRangeException。我不知道为什么,因为卷曲线是36个字符长。这里重要的一点是
卷曲线。子字符串(23)
,因为第二个参数是子字符串的长度,而不是最后一个索引。
public static void Main()
{
    string value="abcdefghijklmnopqrstuvwxyz1234567890";
    newString(value);
}

public static void newString(string curLine)
{
     string[] parsedString = new string[3];
     parsedString[0] = "string";
     parsedString[1] = curLine.Substring(7, 12);
     Console.WriteLine(parsedString[1]);
     parsedString[2] = curLine.Substring(23); // Changes here
     Console.WriteLine(parsedString[2]);

     Console.WriteLine("Done");
     //Form2.codeOutput.Text += "test";
}
Substring(Int32, Int32)
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

public string Substring (int startIndex, int length);

Parameters:
startIndex
Int32
The zero-based starting character position of a substring in this instance.

length
Int32
The number of characters in the substring.