苦苦挣扎的基础VB.net。可变加法

时间:2014-02-06 18:24:52

标签: vb.net variables

我正在尝试设置我的表单程序,因此如果用户无法登录3次(链接到数据库),它会关闭程序。但是,我在编程方面有点废话而且我无法获得变量以实际保留我尝试使用的附加内容?

Private Sub Login_Click(sender As Object, e As EventArgs) Handles Login.Click
    Dim uname, pass As String
    Dim attempt As Integer = 0
    ' Warns the user if they have missed out login information.
    If UserNameBox.Text = "" Or PasswordBox.Text = "" Then
        MessageBox.Show("Please ensure you have entered your username and password", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        uname = UserNameBox.Text
        pass = PasswordBox.Text
        GetFilteredData("username = '" & uname & "' AND password = '" & pass & "'")
        If CountRecords() = 1 Then
            MsgBox("Logged In!")
        Else
            MsgBox("Incorrect Credentials!")
            attempt = attempt + 1 ' <-- Main Issue is here
            If attempt = 4 Then
                Application.Exit()
            End If
        End If
    End If
End Sub

任何帮助都会很棒。谢谢:D

2 个答案:

答案 0 :(得分:3)

您在attempt事件处理程序中声明了Login_Click变量。因此,每次引发Login_Click事件时,您都将其初始化为0.

Dim attempt As Integer = 0

尝试将其移动到外部范围,例如使其成为Class的成员。

答案 1 :(得分:0)

这应该有效。如果你想让所有潜艇都可以访问变量,那么就把它当作类的根。

Private attempt As Integer = 0

Private Sub Login_Click(sender As Object, e As EventArgs) Handles Login.Click
Dim uname, pass As String

' Warns the user if they have missed out login information.
If UserNameBox.Text = "" Or PasswordBox.Text = "" Then
    MessageBox.Show("Please ensure you have entered your username and password", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
    uname = UserNameBox.Text
    pass = PasswordBox.Text
    GetFilteredData("username = '" & uname & "' AND password = '" & pass & "'")
    If CountRecords() = 1 Then
        MsgBox("Logged In!")
    Else
        MsgBox("Incorrect Credentials!")
        attempt = attempt + 1 ' <-- Main Issue is here
        If attempt = 4 Then
            Application.Exit()
        End If
    End If
End If
End Sub