简体   繁体   English

C#字符串串联OutOfRange错误

[英]C# String Concatenation OutOfRange error

I'm utilizing Get String functions from a "Label" class to put together lines to print on a bitmap. 我正在利用“ Label”类中的Get String函数将行放在一起以打印在位图上。 The program compiles fine, and the previous form passes the LabelQueue properly (it would appear with no issue before I tried to print the bitmap). 该程序可以正常编译,并且以前的表单可以正确传递LabelQueue(在尝试打印位图之前,它不会出现任何问题)。 All the code of this particular initializer/constructor is below. 下面是该特定初始化程序/构造函数的所有代码。 The erroneous lines of code are the final three lines of the function before the "c++". 错误的代码行是函数“ c ++”之前的最后三行。

Let me know if you need me to add any more necessary code. 让我知道是否需要我添加更多必要的代码。

I'm getting an IndexOutofRange exception, claiming it was outside of the bounds of the array. 我收到一个IndexOutofRange异常,声称它不在数组的范围内。

private LabelQueue lq;
    public Print(LabelQueue queue)
    {
        InitializeComponent();
        lq = queue;
        pictureBox1.Image = new Bitmap(2550, 3300);

        System.Drawing.Graphics formGraphics = this.CreateGraphics();
        System.Drawing.Font textFont = new System.Drawing.Font("Times New Roman", 8);
        System.Drawing.SolidBrush textBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
        System.Drawing.StringFormat textFormat = new System.Drawing.StringFormat();

        int x, y, c = 0;

        while (c < 30)
        {
            // Get coordinates for where to put values.
            x = ((c % 3) * 600) + 300;
            // Accounts for column gap
            if (c % 3 > 0)
                x = x + ((c % 3) - 1) * 75;
            y = ((c % 10) * 270) + 300;

            string firstLine, secondLine, thirdLine;

            firstLine = lq.labels[c].GetLastName() + ", " + lq.labels[c].GetFirstName() + " " + lq.labels[c].GetMiddleName();
            secondLine = lq.labels[c].GetNewStreet();
            thirdLine = lq.labels[c].GetNewCity() + ", " + lq.labels[c].GetNewState() + lq.labels[c].GetNewZIP() + lq.labels[c].GetNewCountry();

            formGraphics.DrawString(firstLine, textFont, textBrush, x, y, textFormat);  // Line turning up the error
            formGraphics.DrawString(firstLine, textFont, textBrush, x, y + 10, textFormat);  // Naturally, both these lines would need to be fixed too
            formGraphics.DrawString(firstLine, textFont, textBrush, x, y + 20, textFormat);

            c++;
        }

    }

Without a good, minimal , complete code example that reliably demonstrates the problem, it's impossible to know for sure the exact fix you need. 没有一个很好, 最少完整的代码示例来可靠地演示该问题,就不可能确定您所需要的确切修复程序。

However, based on the information on this question and the comments so far, it appears that you simply aren't limiting your loop correctly. 但是,根据到目前为止有关该问题的信息和评论,您似乎并没有正确地限制循环。 The while statement should look like this: while语句应如下所示:

while (c < lq.Count())

Note that the above uses the Enumerable.Count() extension method. 请注意,上面使用了Enumerable.Count()扩展方法。 I chose that as the answer, because you didn't include the declaration/implementation of your LabelQueue object, so there's no way to know for sure what the correct syntax would be, but the extension method is likely to work because pretty much any reasonable collection type that supports an indexer will implement some interface that allows the Enumerable.Count() method to work well. 我选择它作为答案,因为您没有包括LabelQueue对象的声明/实现,因此无法确定确切的语法,但是扩展方法可能会起作用,因为几乎所有合理的方法都可以。支持索引器的集合类型将实现一些接口,该接口允许Enumerable.Count()方法正常工作。

That said, your type probably has a Count property, which you can use instead of the Count() extension method. 也就是说,您的类型可能具有Count属性,您可以使用它来代替Count()扩展方法。 Either will work equally well. 两者都将同样有效。


Finally, for future reference it is fine to answer your own question. 最后,可以回答您自己的问题,以供将来参考。 It's just that your answer should actually be an answer . 只是您的答案实际上应该是一个答案 Ie it needs to explain clearly what was wrong, and what you did to fix it. 即,它需要清楚地解释什么地方出了问题,以及您做了什么修复。 Writing "Issue is fixed" doesn't count as an answer. 撰写“问题已解决”不算作答案。

For that matter, if you don't like my answer here and you want to write your own, you can still do that. 因此,如果您在这里不喜欢我的答案,并且想要编写自己的答案,仍然可以这样做。 You can even accept your own answer instead of mine if you'd rather. 如果愿意,您甚至可以接受自己的答案,而不是我的答案。 Just make sure it's a real answer. 只要确保这是一个真实的答案即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM