调用数组作为参数VB.NET时出错

时间:2014-04-12 16:57:41

标签: vb.net

好的,首先我在这里有一个数组

  Dim JD = {"0", "LIE"} 

然后我有3个文本框,我也放在数组

  Dim ColJD1 As TextBox() = {Me.JD1_1, Me.JD1_2, Me.JD1_3}

然后我在子程序中使用这些数组:

     Sub fillword(ByVal num1, ByVal qtychar, ByVal num2, ByVal i, ByVal Coltxtbox)

     If Strings.Mid(My.Computer.FileSystem.ReadAllText("SAVE.txt"), Strings.InStr(My.Computer.FileSystem.ReadAllText("SAVE.txt"), num1), qtychar) = num2 & ".True" Then

        For test = 1 To Strings.Len(JD(i))
            For Each txtbox In Coltxtbox
                Dim a As String = Strings.Mid(JD(i), test, 1)
                MsgBox(a)
                txtbox.Text = i     => here is the problem
                i = i + 1
            Next
        Next
    End If

  End Sub

   Sub loadgame1()
      fillword("1", 6, "1", 1, ColJD1)
   End Sub

   Private Sub Button_click bla bla 
      loadgame1()
   End Sub

它的投掷错误: "对象变量或未设置块变量。"

非常感谢任何形式的帮助

@ w0051977实际上变量名是" test"而不是" uji"

收到汉斯建议后,我确​​实修改了代码,但仍然会抛出错误: 我改变了代码:

    Dim JD = {"0", "RIA"}
    Dim ColJD1 As TextBox()

    Sub fillarray()
     InitializeComponent()
        ColJD1 = {JD1_1, JD1_2, JD1_3}
    End Sub

     Sub fillword(ByVal num1, ByVal qtychar, ByVal num2, ByVal i, ByVal Coltxtbox)
     fillarray()
      If Strings.Mid(My.Computer.FileSystem.ReadAllText("SAVE.txt"), Strings.InStr(My.Computer.FileSystem.ReadAllText("SAVE.txt"), num1), qtychar) = num2 & ".True" Then
       For test = 1 To Strings.Len(JD(i))
               For Each txtbox In Coltxtbox => Now the problem moves here , it throws error "Object reference not set to an instance of an object."
                  Dim a As String = Strings.Mid(JD(i), test, 1)
                  MsgBox(a)
                  txtbox.Text = i     
                  i = i + 1
               Next
            Next
         End If
      End Sub

      Sub loadgame1()
         fillword("1", 6, "1", 1, ColJD1)
      End Sub

      Private Sub Button_click bla bla 
        loadgame1()
      End Sub

1 个答案:

答案 0 :(得分:1)

使用调试器,您可以检查变量以找到Nothing。

很可能是ColJD1数组,无法看到调用的范围,但这是非常常见的错误。在运行InitializeComponent()方法之后,像Me.JD1_1这样的成员才会获得一个值。该变量很快就会初始化,之前该方法运行。所以它只包含Nothing元素。你需要这样写:

Dim ColJD1 As TextBox()

Public Sub New
    InitializeComponent()
    ColJD1 = {Me.JD1_1, Me.JD1_2, Me.JD1_3}
End Sub

Sub New是需要添加的构造函数。