VB.NET OverflowException未处理

时间:2014-04-16 08:03:18

标签: vb.net graphics

我在VB 2008中遇到一个问题,它给出了一个错误:“OverflowException未处理。” 在那段代码中:错误突出显示Next b

        Dim gfx As Graphics
        Dim a,r,g,b As byte
        Dim left As Integer
        Dim lStep As Integer = 1

        For left = 0 To Me.ClientRectangle.Height Step lStep

            For a = 1 To 255
                For r = 1 To 255
                    For g = 1 To 255
                        For b = 1 To 255
                            gfx.DrawLine(New Pen(Color.FromArgb(a, r, g, b)), 0, left, Me.ClientRectangle.Width, left)

                        Next b

                    Next g
                Next r
            Next a

1 个答案:

答案 0 :(得分:2)

    Dim a,r,g,b As byte

问题开始的地方。 For循环从1递增到255,当值达到256时停止。但是对于Byte来说这是不可能的,它只能存储0到255之间的值。当Next语句尝试从中增加它时Kaboom 255到256。

只需声明As Integer即可。它不仅解决了溢出问题,而且速度更快。