首页 > 解决方案 > 为什么将数据移动到文本字段时,索引超出了数组的范围

问题描述

这是我的代码 - 本部分正在查看来自 NMEA GPS 设备的消息

          Try
            degreeString = Words(3).Substring(0, 2)
            LatDD = CDec(degreeString)
            MinuteString = (Words(3).Substring(2, 8))
            LatDM = CDec(CInt(MinuteString) / 60)
            LatDecimalDegrees = LatDD + LatDM
            If Words(4) = "S" Then
                LatDecimalDegrees *= -1
            End If
           Catch ex As Exception
            errorCount = errorCount + 1
            ErrorDisplay.Clear()
            ErrorDisplay.Text = ToString(errorCount)
            GPS_WordOK = False
            Exit Function
        End Try code here`Try
            degreeString = Words(3).Substring(0, 2)
            LatDD = CDec(degreeString)
            MinuteString = (Words(3).Substring(2, 8))
            LatDM = CDec(CInt(MinuteString) / 60)
            LatDecimalDegrees = LatDD + LatDM
            If Words(4) = "S" Then
                LatDecimalDegrees *= -1
            End If
        Catch ex As Exception
            errorCount = errorCount + 1
            ErrorDisplay.Clear()
            ErrorDisplay.Text = ToString(errorCount)
            GPS_WordOK = False
            Exit Function
        End Try

异常处理中在这一行抛出错误

    ErrorDisplay.Text = ToString(errorCount)

error_count 的值为 49

我看不到失败行中隐含数组的位置

导致进入异常处理程序的错误由语句抛出

    LatDM = CDec(CInt(MinuteString) / 60)

字符串值为 41*68028

我相信线路上的一些噪音导致小数被读取为星号并且除法失败。此错误恢复在 24 小时内成功进行了 48 次。

标签: vb.net

解决方案


使用任一

ErrorDisplay.Text = errorCount.ToString

或者

ErrorDisplay.Text = CStr(errorCount)


推荐阅读