If和Else语句在Visual Basic上

时间:2017-03-11 14:09:15

标签: vb.net if-statement

我无法确定在Visual Basic 2010 Express中需要安排这些if和else语句的顺序。正如您可以从代码中看到的那样,当它正常工作时,程序应该在外部显示文本文件中的数据。如果未找到此数据,则应显示标签中的错误消息。 谢谢你的帮助。

代码:

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim filename As String = "..\..\usernamepassword.txt"
    Dim objreader As New System.IO.StreamReader(filename)
    Dim contents As String
    Dim check As String
    Dim checkfile As New System.IO.StreamReader("..\..\checkfile.txt")
    check = checkfile.ReadToEnd
    For i As Integer = 1 To check
        contents = objreader.ReadLine
        Dim data As New List(Of String)(contents.Split(","))
        If forename.Text = data(2) Then
     'first if statement is here \/
            If surname.Text = data(3) Then
                fullname.Text = (data(2) & " " & data(3))
                dob.Text = data(4)
                phone.Text = data(5)
                address.Text = data(6)
                password.Text = data(1)
           'else statement \/
            Else
                Label2.Text = "Sorry, no result found for this student."
                Label5.Text = "Please note this system is caps sensitive."
            End If
        End If
    Next i
    Dim s As String = ""
    forename.Text = s
    surname.Text = s
End Sub

结束班

2 个答案:

答案 0 :(得分:0)

你应该像这样处理它

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filename As String = "..\..\usernamepassword.txt"
Dim found as Boolean = False
Dim objreader As New System.IO.StreamReader(filename)
Dim contents As String
Dim check As String
Dim checkfile As New System.IO.StreamReader("..\..\checkfile.txt")
check = checkfile.ReadToEnd
For i As Integer = 1 To check
    contents = objreader.ReadLine
    Dim data As New List(Of String)(contents.Split(","))
    If forename.Text = data(2) Then
 'first if statement is here \/
        If surname.Text = data(3) Then
            fullname.Text = (data(2) & " " & data(3))
            dob.Text = data(4)
            phone.Text = data(5)
            address.Text = data(6)
            password.Text = data(1)
            found = True
            Exit For
        End If
    End If
Next i
If found = False Then
   Label2.Text = "Sorry, no result found for this student."
   Label5.Text = "Please note this system is caps sensitive."
End If
Dim s As String = ""
forename.Text = s
surname.Text = s
End Sub

答案 1 :(得分:0)

当它应该是整数时,检查已被声明为字符串。 将option strict设置为on并推断为off