如果数组是空的错误

时间:2018-03-27 19:38:24

标签: vb.net

数组是一个声明为字符串的9位数组,我有9个用户可以输入数据的文本框,每个文本框写入数组中的一个变量。

我正在尝试停止填充数组,并在用户填写所有9个文本框时移动打印,或者当他按下"写入文件时停止填写#34;写入文件&#34 ;按钮。从我的调试点来看,看起来我已经到了"对于"循环,但程序崩溃没有错误,我可以做出头或尾(不是语法或变量)...任何人都可以看到我错过了什么?

由于

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim FILE_NAME As String = "C:\Users\foo\test2.txt"

    Dim i As Integer
    Dim j As Integer
    Dim aryText(9) As String

    MessageBox.Show(j)
    aryText(0) = "[" & TextBox1.Text & "]"
    j = 0
    MessageBox.Show(j)
    If String.IsNullOrWhiteSpace(TextBox2.Text) Then
        End
    Else
        aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If

    'If TextBox3.Text IsNot Nothing Then
    If String.IsNullOrWhiteSpace(TextBox3.Text) Then
        End
    Else
        aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If


    If String.IsNullOrWhiteSpace(TextBox4.Text) Then
        End
    Else
        aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If


    If String.IsNullOrWhiteSpace(TextBox5.Text) Then
        End
    Else
        aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If


    If String.IsNullOrWhiteSpace(TextBox6.Text) Then
        End
    Else

        aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If


    If String.IsNullOrWhiteSpace(TextBox7.Text) Then
        End
    Else
        aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If


    If String.IsNullOrWhiteSpace(TextBox8.Text) Then
        End
    Else

        aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If


    If String.IsNullOrWhiteSpace(TextBox9.Text) Then
        End
    Else
        aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
        j = j + 1
        MessageBox.Show(j)
    End If


    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)

    MessageBox.Show(j)
    For i = 0 To j

        objWriter.WriteLine(aryText(j))
        i = i + 1
    Next

    objWriter.Close()
    MessageBox.Show("Text written to file")





End Sub

1 个答案:

答案 0 :(得分:0)

  1. 请不要使用MessageBoxes进行调试。 Visual Studio有一个可爱的调试器。
  2. VB.net数组声明为Dim myArray(UpperBound)As String。因此,对于9个文本框,您的上限为8,索引为0到8
  3. 您希望Exit Sub not End。
  4. A对于x = 0到y ...接下来的工作原理是自动递增x(即下一个意味着,下一个x)默认值为1,但您可以通过添加步骤来更改它。如果你试图改变你,你会弄得一团糟。你的For ... Next只会将数组中的最后一个值写入9次(aryText(j)),因为j不会改变。不要在循环中增加x;当调用Next时,它会自动递增。
  5. 不需要弄乱我和j;只需使用For Each
  6. 为您的流编写器使用代码块可确保释放所有资源。

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim FILE_NAME As String = "C:\Users\foo\test2.txt"
            Dim aryText(8) As String
            Dim msg As String = "Please fill in all the boxes,"
            If String.IsNullOrWhiteSpace(TextBox1.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(0) = "[" & TextBox1.Text & "]"
            End If
            If String.IsNullOrWhiteSpace(TextBox2.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
            End If
            If String.IsNullOrWhiteSpace(TextBox3.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
            End If
            If String.IsNullOrWhiteSpace(TextBox4.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
            End If
            If String.IsNullOrWhiteSpace(TextBox5.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
            End If
            If String.IsNullOrWhiteSpace(TextBox6.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
            End If
            If String.IsNullOrWhiteSpace(TextBox7.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
            End If
            If String.IsNullOrWhiteSpace(TextBox8.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
            End If
            If String.IsNullOrWhiteSpace(TextBox9.Text) Then
                MessageBox.Show(msg)
                Exit Sub
            Else
                aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
            End If
            Using objWriter As New System.IO.StreamWriter(FILE_NAME, True)
                For Each s As String In aryText
                    objWriter.WriteLine(s)
                Next
            End Using
            MessageBox.Show("Text written to file")
        End Sub