繁体   English   中英

VB.net ByVal和ByRef不起作用

[英]VB.net ByVal and ByRef doesnt work

所以我有几个子程序,我想在它们之间共享变量...我是编程新手,我不知道如何使用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

如果用户名和密码在子目录中进行了检查,而仅在按钮单击事件中进行了检查,则可以大大降低复杂性。 您无需在每次有人更改文本框的值时检查它们是否正确。

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

如果您想将BlnCorrectPasswordBlnCorrectLogin与其他Subs或Function一起使用, BlnCorrectPassword它们定义为该类中的全局变量,例如

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时只需检查输入的文本,那样就不需要布尔变量和少两个子

该代码仍然很丑陋,并且绝对可以简化:

1.) blncorrectlogin = True And blncorrectpassword = True =>您无需编写= TRUE; 不应使用AndAlso ,请使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM