windowsapplication1.exe附加信息中出现未处理的'system.indexoutofrangeexception'类型异常:index超出了数组的范围。 [英] An unhandled exception of type 'system.indexoutofrangeexception' occurred in windowsapplication1.exe additional information: index was outside the bounds of the array.

查看:106
本文介绍了windowsapplication1.exe附加信息中出现未处理的'system.indexoutofrangeexception'类型异常:index超出了数组的范围。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dim ECUData(27) as integer
dim RXArray(27) as integer





它显示我在ECUdata和Rxarray的12号索引上的错误



我在开始时我是真的想学习编程的东西。



我尝试过:





it shows me error on index 12th of ECUdata and Rxarray both

I am at the start and I am really trying to learn things for programming.

What I have tried:

Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPorts.DataReceived
        If COMPorts.IsOpen = False Then
            COMPorts.Open()
        End If
        Dim RXByte As Byte

        RXCnt = 0
        Do

            RXByte = COMPorts.ReadByte

            RXArray(RXCnt) = Chr(RXByte) ' Convert each byte to two hexadecimal characters
            RXCnt = RXCnt + 1
            ECUData(RXCnt) = RXByte  <---------Getting Error in This Line
        Loop Until (COMPorts.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime
        '----- End of communication protocol handling -------------------------------------------------------------
        Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
    End Sub

推荐答案

使用调试器。

在函数的第一行放置断点,然后运行代码通过调试器。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一种新的(非常非常有用的)技能:调试!



我怀疑是因为你没有在任何地方重置RXCnt,它只是溢出你的阵列。
Use the debugger.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

I suspect that because you don't reset RXCnt anywhere, it just overflows your array.


你知道如果

You know that is going to happen if
COMPorts.BytesToRead > 27



您可以重新调整阵列,以确保所有传入的数据都适合或丢弃这些阵列已满后的任何数据。


You either redim your arrays to make sure all the incoming data can fit or discard any data coming after those arrays are full.


这篇关于windowsapplication1.exe附加信息中出现未处理的'system.indexoutofrangeexception'类型异常:index超出了数组的范围。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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