索引和长度必须引用string.parameter name中的位置:length [英] Index and length must refer to a location within the string.parameter name: length

查看:272
本文介绍了索引和长度必须引用string.parameter name中的位置:length的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public override void CreateNewOutputRows()

{

StreamReader sr = new StreamReader(fullFilePath);



int lineIndex = 0;

string line;

while(!sr.EndOfStream)

{

line = sr.ReadLine();



if(line.Substring(0,1).ToString()==P)

{

HeaderBuffer.AddRow();

HeaderBuffer.Indicator = line.Substring(0,1).ToString();

}



lineIndex = lineIndex + 1;



}

sr.Close ();

}



我的尝试:



我只需要从平面文件中读取标题行第1个字符并写入表格。

请帮助我为什么会收到此错误索引和长度必须指向某个位置在字符串中。参数名称:长度。



谢谢

public override void CreateNewOutputRows()
{
StreamReader sr = new StreamReader(fullFilePath);

int lineIndex = 0;
string line;
while (!sr.EndOfStream)
{
line = sr.ReadLine();

if (line.Substring(0,1).ToString() == "P")
{
HeaderBuffer.AddRow();
HeaderBuffer.Indicator = line.Substring(0,1).ToString();
}

lineIndex = lineIndex + 1;

}
sr.Close();
}

What I have tried:

I just need to read the header line 1st character from a flat file and write to table.
Please help why am I getting this error "Index and length must refer to a location within the string. Parameter name: length ".

Thanks

推荐答案

如果你只是在下面运行代码,你可以轻松找到这个调试器。



您的代码正在处理您正在阅读的文本文件的每一行,就好像它中包含一个或多个字符一样。事实并非如此。你的文件中有空白行,长度为0,因此,你得到的例外。



现在,你打算做什么它?如何在对线做任何事情之前检查长度为0?
You could have easily found this if you just ran the code under the debugger.

Your code is treating every line of the text file you're reading as if it had ONE OR MORE characters in it. That's just not the case. You've got blank lines in your file that have a length of 0, hence, the exception you're getting.

Now, what are you going to do about it? How about checking for a length of 0 before you do anything with the line?


引用:

为什么我收到此错误索引和长度必须引用字符串中的位置。参数名称:长度。

why am I getting this error "Index and length must refer to a location within the string. Parameter name: length ".



错误消息告诉您尝试在字符串外部子字符串。




The error message tells you that you try to substring outside of the string.
In

line.Substring(0,1).ToString()



你忘了检查该行至少包含1个字符。

使用调试器,您可以在执行时观看程序。



您的代码行为不符合您的预期,您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在执行,您的任务是与应该执行的操作进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只显示你的代码在做什么,你的任务是与它应该做的事情进行比较。


you forgot to check that line contain at least 1 char.
With the debugger, you can watch the program as it perform.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于索引和长度必须引用string.parameter name中的位置:length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆