VB.net ByVal和ByRef不起作用

时间:2016-01-19 14:25:57

标签: vb.net login

所以我有几个潜艇,我想在它们之间共享变量......我是编程新手,我不懂如何使用ByVal和ByRef。我试图创建一个登录页面。

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles LoginButton.Click (ByRef BlnCorrectPassword As Boolean, ByRef BlnCorrectLogin As Boolean)


    If blncorrectlogin = True And blncorrectpassword = True Then
        MsgBox("This part is still being developed!") ' I will make this form close and open a new form.
    Else
        MsgBox("The login or password is incorrect, please try again.")
    End If
End Sub

Private Sub Label1_Click(sender As Object, e As EventArgs)

End Sub

Private Sub Label1_Click_1(sender As Object, e As EventArgs) Handles Label1.Click

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Dim BlnCorrectLogin As Boolean

    If TextBox1.Text = "login" Then
        BlnCorrectLogin = True

    End If
End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
    Dim BlnCorrectPassword As Boolean

    If TextBox1.Text = "password" Then
        BlnCorrectPassword = True

    End If
   End Sub
 End Class

3 个答案:

答案 0 :(得分:1)

如果在子网站中检查用户名和密码,并且只在按钮点击事件上进行检查,则可以大大降低复杂性。每次有人更改文本框的值时,您都不需要检查它们是否正确。

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

    If CheckLoginDetails() = True Then
        MsgBox("This part is still being developed!") ' I will make this form close and open a new form.
    Else
        MsgBox("The login or password is incorrect, please try again.")
    End If
End Sub

Private Function CheckLoginDetails() As Boolean
    If TextBox1.Text = "login" AndAlso TextBox2.Text = "password" Then
        Return True
    Else
        Return False
    End If
End Function

答案 1 :(得分:0)

如果您想将BlnCorrectPasswordBlnCorrectLogin与其他子或函数一起使用,则将它们定义为该类中的全局变量

Public Class Form1

  Private BlnCorrectPassword as Boolean = False
  Private BlnCorrectLogin as Boolean = False

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

    If blncorrectlogin = True And blncorrectpassword = True Then
        MsgBox("This part is still being developed!") ' I will make this form close and open a new form.
    Else
        MsgBox("The login or password is incorrect, please try again.")
    End If

  End Sub

  Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    If TextBox1.Text = "login" Then
        BlnCorrectLogin = True
    End If

  End Sub

  Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

    If TextBox1.Text = "password" Then
        BlnCorrectPassword = True
    End If

  End Sub

End Class
编辑:只要你不使用它,你也不必用每个字母(TextChanged)检查输入的文本,所以当你单击LoginButton时,只需检查输入的文本,这样你就不会这样做。需要布尔变量并减少两个Subs

答案 2 :(得分:0)

代码仍然很难看,绝对可以简化:

1。)blncorrectlogin = True And blncorrectpassword = True =>你不需要写= TRUE;不应使用AND,请使用AndAlso

2。)MsgBox已弃用。改为使用MessageBox.Show。

3。)Textchanged可以连接起来。如果像这样:

 If TextBox1.Text = "login" Then
        BlnCorrectLogin = True
    End If

完全是废话和无知。

4.。)字符串不应与“=”比较,请使用.Equals

Option Strict On 'Always write this
Option Infer Off 'Always write this
Imports System.IO
Public Class Form1
    Private BlnCorrectPassword As Boolean = False
    Private BlnCorrectLogin As Boolean = False
    Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        If sender.Equals(TextBox1) Then
            BlnCorrectLogin = TextBox1.Text.Equals("login")
        End If

        If sender.Equals(TextBox2) Then
            BlnCorrectPassword = TextBox2.Text.Equals("password")
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not (BlnCorrectLogin AndAlso BlnCorrectPassword) Then
            'do some stuff
            MessageBox.Show("The login or password is incorrect, please try again.")
            Exit Sub 'exit the sub
        End If
        'login data correct
        MessageBox.Show("This part is still being developed!")

    End Sub
End Class