帮助:索引在vb 2010中超出了数组的范围 [英] HELP: Index was outside the bounds of the array in vb 2010

查看:102
本文介绍了帮助:索引在vb 2010中超出了数组的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有点卡在这里因为我不是很好的编程而且还在学习:)



我遇到了错误索引超出界限的数组。我放置了msgboxes,以便我真的知道它是如何流动的(我没有使用调试工具,因为我不想要它。:))



无论如何我到目前为止所做的解决方案是:

1.我使用

Hello guys, im a bit stuck here as im not that good in programming and still learning :)

im stuck with an error "Index was outside the bounds of the array". i placed msgboxes so that i could really know how it is flowing, (i did'nt use the debug tool because i dont want it. :) )

anyhow the solutions i have made so far are:
1. i used

Option Explicit on

=没有用。

2.试图减少无限( arrayTextFile)

= didnt worked.
2. tried to lessen the ubound(arrayTextFile)

For i = 1 To UBound(ArrayTextFile) - 2

它再次工作

3.我用过这种检查,我真的不知道它做了什么。但它让我失望:(



it ddnt work again
3. i used this kind of checking, i really dont know what it did. but it failed me :(

Dim placename As String
If ListView1.Items.Count > 0 Then
    placename = ListView1.Items.Count = 0
Else
    placename = String.Empty
End If





i知道我错过了一些我无法通过代码看到的东西,

所以任何人都可以帮助我。谢谢你们。

这里是函数的代码。



我通过MsgBox后(inside with - & ListView1.Items.Count)我指示了adady到catch并发布了消息索引超出阵列范围



i know i am missing something i just cant see through codes,
so can anybody please help me. thank you guys.
here's the code for the function.

after i pass MsgBox("inside with - " & ListView1.Items.Count) Im directed aleady to the catch and posted with the message "Index was outside the bounds of the array"

Try
            Dim ArrayTextFile() As String
            Dim lineOfText As String
            Dim i As Integer

            lineOfText = rcvdata.ToString
            ArrayTextFile = Split(lineOfText, "+CMGL", , CompareMethod.Text)
            MsgBox("did i pass here?" & i)
            MsgBox(UBound(ArrayTextFile))
            For i = 1 To UBound(ArrayTextFile)
                Dim input As String = ArrayTextFile(i)
                Dim result() As String
                Dim pattern As String = "(:)|(,"")|("","")"
               
 MsgBox("did i pass here? -pattern")
                result = Regex.Split(input, pattern)

                Dim lvi As New ListViewItem
                Dim concat() As String
                MsgBox("did i pass here? -lvi and concat")

                With (ListView1.Items.Add("null"))
                    MsgBox("inside with - " & ListView1.Items.Count)
                    'for item 1
                    .SubItems.AddRange(New String() {result(2)})
                    MsgBox("did i pass here? result 2")
                    'for item 2
                    .SubItems.AddRange(New String() {result(4)})
                    MsgBox("did i pass here? -result 4")
                    'extract numbers
                    Dim myString, position As String
                    myString = result(6)
                    position = myString.Length - 2
                    myString = myString.Remove(position, 2)
                    .SubItems.Add(myString)
                    MsgBox("did i pass here? -result5")
                    'for date/time
                    concat = New String() {result(8) & result(9) & result(10) & result(11) & result(12).Substring(0, 2)}
                    .SubItems.AddRange(concat)
                    'message extracted
                    Dim lineOfTexts As String
                    Dim arrayTextFiles() As String
                    lineOfTexts = ArrayTextFile(i)
                    arrayTextFiles = Split(lineOfTexts, "+32", , CompareMethod.Text)
                    .SubItems.Add(arrayTextFiles(i))
                End With
                MsgBox("did i pass here? going to the top to loop again")
            Next
            MsgBox("loop finish")
        Catch ex As Exception
            MsgBox("what is wrong?" & ex.Message)
        End Try
    End Sub

推荐答案

首先 - 使用调试器如果你想被认真对待。你不仅可以很快确定错误发生的位置,还可以轻松检查局部变量,以帮助确定错误。



显然,数组结果只有2个元素,并且您尝试使用 result(2)<访问第三个元素/ code>



记住数组等往往是从零开始的,即result(0)将是第一个不是结果的元素(1)
Firstly - use the debugger if you want to be taken seriously. Not only can you determine exactly where the error occurred very quickly you can also easily examine the local variables to help determine what is wrong.

Clearly array result only has 2 elements and you are trying to access a third with result(2)

Remember arrays etc tend to be zero-based i.e. result(0) would be the first element not result(1)


这篇关于帮助:索引在vb 2010中超出了数组的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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