此代码的意外结果

发布于 2024-10-25 02:02:42 字数 1109 浏览 6 评论 0原文

我有一个对象声明为:

private string SourceProgram;

基本上我正在尝试使用下面的代码解析一些内容:

 private void LabelScan(System.IO.BinaryWriter OutputFile, bool IsLabelScan)
        {

            if (char.IsLetter(SourceProgram[CurrentNdx]))
            {
               if (IsLabelScan) LabelTable.Add(GetLabelName(), AsLength);
                while (SourceProgram[CurrentNdx] != '\n')
                    CurrentNdx++;
                CurrentNdx++;
                return;
            }
            EatWhiteSpaces();
            ReadMneumonic(OutputFile, IsLabelScan);
        }

但是我在执行时遇到错误:

-       SourceProgram[CurrentNdx]   
'SourceProgram[CurrentNdx]' threw an exception of 
type 'System.IndexOutOfRangeException'  char {System.IndexOutOfRangeException}

-       base    {"Index was outside the bounds of the array."}
    System.SystemException {System.IndexOutOfRangeException}

并且 CurrentNdx 的值是 46。

出了什么问题。是 SourceProgram 的字符串变量 length 46 ?

如果是,如何修复此代码?

I have an object declared as:

private string SourceProgram;

Basically i am trying to parse some stuff using the code below:

 private void LabelScan(System.IO.BinaryWriter OutputFile, bool IsLabelScan)
        {

            if (char.IsLetter(SourceProgram[CurrentNdx]))
            {
               if (IsLabelScan) LabelTable.Add(GetLabelName(), AsLength);
                while (SourceProgram[CurrentNdx] != '\n')
                    CurrentNdx++;
                CurrentNdx++;
                return;
            }
            EatWhiteSpaces();
            ReadMneumonic(OutputFile, IsLabelScan);
        }

However i get an error on execution:

-       SourceProgram[CurrentNdx]   
'SourceProgram[CurrentNdx]' threw an exception of 
type 'System.IndexOutOfRangeException'  char {System.IndexOutOfRangeException}

-       base    {"Index was outside the bounds of the array."}
    System.SystemException {System.IndexOutOfRangeException}

And the Value of CurrentNdx is 46.

What has gone wrong. Is the string variable SourceProgram of length < 46 ?

If yes, how to fix this code?

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

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

发布评论

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

评论(3

自找没趣 2024-11-01 02:02:42

是的,此错误表明 SourceProgram 少于 47 个字符。虽然没有看到 SourceProgram 的内容,但我们能告诉您的几乎就是这些。

Yes, this error suggests that SourceProgram has less than 47 characters. That's pretty much all we can tell you though without seeing the contents of SourceProgram.

几度春秋 2024-11-01 02:02:42

该代码似乎在字符串 SourceProgram 中查找换行符。也许 SourceProgram 不包含 \n?

当然,最好使用 intposition = SourceProgram.indexOf("\n") 来查找 \n 的位置

此外,在此代码中您似乎没有将 CurrentNdx 重置为零,这可能在其他地方需要

The code seems to look for a new line character in the string SourceProgram. Maybe SourceProgram doesn't contain a \n?

Surely it would be better to use int position = SourceProgram.indexOf("\n") to find the position of the \n

Also, you don't appear to be resetting CurrentNdx to zero in this code, which would likely be required elsewhere

作妖 2024-11-01 02:02:42
while (SourceProgram[CurrentNdx] != '\n')
                    CurrentNdx++;

也许您的 SourceProgram 字符串不包含换行符,或者在 CurrentNdx 超出字符串中的任何换行符之后调用该函数。

while (SourceProgram[CurrentNdx] != '\n')
                    CurrentNdx++;

maybe your SourceProgram string doesn't contain a newline, or the function is called after CurrentNdx is beyond any newlines in the string.

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