将字符串拆分为数组时出错

时间:2015-07-20 09:14:37

标签: arrays vb.net string

基本上,除非它在消息框中,否则我无法修剪它...这很难解释。 这里有2张图片: 1. http://gyazo.com/83b0a996e607f7013d998f6f800650f1 2. http://gyazo.com/e1fe9d8adb4a522479f6621d29e90e9d

Dim value As String = ary(0).Trim()
        'Dim value1 As String = ary(1).Trim()

        Dim R As String
        Dim G As String
        Dim B As String

        Dim outline As String
        Dim outlineColor As String


        R = Chr(34) & "MouseColorR" & Chr(34)
        G = Chr(34) & "MouseColorG" & Chr(34)
        B = Chr(34) & "MouseColorB" & Chr(34)

        outline = Chr(34) & "ThickMouseEdges" & Chr(34)
        outlineColor = Chr(34) & "ThickMouseEdgesPackedColor" & Chr(34)
        'based on the value after the equals sign, do something



        If value = R Then
            MsgBox(ary(1).Trim(Chr(44)))

        ElseIf value = G Then
            MsgBox("finally")

        ElseIf value = B Then
            MsgBox("finally")

        ElseIf value = outline Then
            MsgBox("finally")

这有效^^^^

这不是:

        Dim value As String = ary(0).Trim()
       this is the error---> Dim value1 As String = ary(1).Trim()

        Dim R As String
        Dim G As String
        Dim B As String

        Dim outline As String
        Dim outlineColor As String


        R = Chr(34) & "MouseColorR" & Chr(34)
        G = Chr(34) & "MouseColorG" & Chr(34)
        B = Chr(34) & "MouseColorB" & Chr(34)

        outline = Chr(34) & "ThickMouseEdges" & Chr(34)
        outlineColor = Chr(34) & "ThickMouseEdgesPackedColor" & Chr(34)
        'based on the value after the equals sign, do something



        If value = R Then
            MsgBox(ary(1).Trim(Chr(44)))

        ElseIf value = G Then
            MsgBox("finally")

        ElseIf value = B Then
            MsgBox("finally")

        ElseIf value = outline Then
            MsgBox("finally")

        ElseIf value = outlineColor Then
            MsgBox("finally")

,错误是:类型' System.IndexOutOfRangeException'未处理的异常。发生在Terraria Smart Cursor.exe

其他信息:索引超出了数组的范围。

整个代码:

Public Class Form1

Private Sub NsCheckBox1_CheckedChanged(sender As Object) Handles NsCheckBox1.CheckedChanged

    NsGroupBox2.Enabled = NsCheckBox1.Checked

End Sub

Private Sub NsTrackBar1_Scroll(sender As Object) Handles NsTrackBar1.Scroll
    NsLabel4.Value1 = NsTrackBar1.Value
End Sub

Private Sub NsTrackBar2_Scroll(sender As Object) Handles NsTrackBar2.Scroll
    NsLabel5.Value1 = NsTrackBar2.Value
End Sub

Private Sub NsTrackBar3_Scroll(sender As Object) Handles NsTrackBar3.Scroll
    NsLabel6.Value1 = NsTrackBar3.Value
End Sub

Private Sub NsTrackBar6_Scroll(sender As Object) Handles NsTrackBar6.Scroll
    NsLabel9.Value1 = NsTrackBar6.Value
End Sub

Private Sub NsTrackBar5_Scroll(sender As Object) Handles NsTrackBar5.Scroll
    NsLabel8.Value1 = NsTrackBar5.Value
End Sub

Private Sub NsTrackBar4_Scroll(sender As Object) Handles NsTrackBar4.Scroll
    NsLabel7.Value1 = NsTrackBar4.Value
End Sub

Private Sub NsButton1_Click_1(sender As Object, e As EventArgs) Handles NsButton1.Click
    If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        NsLabel4.Value1 = ColorDialog1.Color.R
        NsLabel5.Value1 = ColorDialog1.Color.G
        NsLabel6.Value1 = ColorDialog1.Color.B

        NsTrackBar1.Value = NsLabel4.Value1
        NsTrackBar2.Value = NsLabel5.Value1
        NsTrackBar3.Value = NsLabel6.Value1
    End If
End Sub

Private Sub NsButton2_Click_1(sender As Object, e As EventArgs) Handles NsButton2.Click
    If ColorDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
        NsLabel9.Value1 = ColorDialog2.Color.R
        NsLabel8.Value1 = ColorDialog2.Color.G
        NsLabel7.Value1 = ColorDialog2.Color.B

        NsTrackBar6.Value = NsLabel9.Value1
        NsTrackBar5.Value = NsLabel8.Value1
        NsTrackBar4.Value = NsLabel7.Value1
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'reads each line from the text file one at a time
    For Each line As String In IO.File.ReadLines("C:\Users\Matthew\Documents\My Games\Terraria\config.json")


        'split the string by equals sign
        Dim ary As String() = line.Split(":")


        Dim value As String = ary(0).Trim()
        Dim value1 As String = ary(1).Trim()

        Dim R As String
        Dim G As String
        Dim B As String

        Dim outline As String
        Dim outlineColor As String


        R = Chr(34) & "MouseColorR" & Chr(34)
        G = Chr(34) & "MouseColorG" & Chr(34)
        B = Chr(34) & "MouseColorB" & Chr(34)

        outline = Chr(34) & "ThickMouseEdges" & Chr(34)
        outlineColor = Chr(34) & "ThickMouseEdgesPackedColor" & Chr(34)
        'based on the value after the equals sign, do something



        If value = R Then
            MsgBox(ary(1).Trim(Chr(44)))

        ElseIf value = G Then
            MsgBox("finally")

        ElseIf value = B Then
            MsgBox("finally")

        ElseIf value = outline Then
            MsgBox("finally")

        ElseIf value = outlineColor Then
            MsgBox("finally")

        End If



    Next
End Sub

结束班

2 个答案:

答案 0 :(得分:0)

您正在访问位置0处的容器,然后处于1处,而不是。屏幕截图中显示的例外情况表明这是一个超出范围的问题。所以你的容器没有你想象的那么大:它只有一个元素。

答案 1 :(得分:0)

您正在通过ary填充Dim ary As String() = line.Split(":")。您正在阅读C:\Users\Matthew\Documents\My Games\Terraria\config.json

中的每一行

该文件的其中一行不包含:。因此Split创建一个只包含一个元素的数组。当你打电话:

Dim value1 As String = ary(1).Trim()

你得到ArgumentOutOfRangeException,因为元素1不存在。

解决方案是测试数组长度,让代码以优雅的方式处理没有:的行。