0

我有一个从表单派生的类,它有用于用户名和密码的文本框以及一个确定按钮。我希望它表现得像一个 InputBox,所以我可以像这样使用它:

Dim Username As String = ""
Dim Password As String = ""
Dim authorization As New Authorization(Username, Password)
authorization.ShowDialog()
    'The user will click OK and I will expect the Username and Password to change based on the user input
MsgBox(Username & " " & Password)

授权类:

Public Class Authorization
    Dim RefUsername As String
    Dim RefPassword As String

    Public Sub New(ByRef Username As String, ByRef Password As String)
        InitializeComponent()

        RefUsername = Username 'I'm trying to pass reference instead of value
        RefPassword = Password 'I'm trying to pass reference instead of value
    End Sub

    Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuthorizeButton.Click
        RefUsername = Username.Text 'I'm trying to change value of variable outside the class
        RefPassword = Password.Text 'I'm trying to change value of variable outside the class
        Me.Close()

    End Sub
End Class

简而言之,当用户单击“确定”时,我想更改类外变量的值,我将如何实现呢?

4

2 回答 2

1
Public Class Authorization
Dim RefUsername As String
Dim RefPassword As String

Public Sub New(ByRef Username As String, ByRef Password As String)
    InitializeComponent()

    RefUsername = Username 'I'm trying to pass reference instead of value
    RefPassword = Password 'I'm trying to pass reference instead of value
End Sub

Private Shared Function PromptUser() As ReturnClass
   Dim currentReturnClass as ReturnClass

   // Dialog Goes Here
   currentReturnClass.UserName = Username.Text
   currentReturnClass.Password = Password.Text
   Me.Close()
End Sub

End Class

private class ReturnClass
   Dim userName as String
   Dim password as String
   Dim userAcceptedDialog as Boolean
   ' Setters and Getters

End Class

您的调用代码:

Dim Username As String = "Default User"
Dim Password As String = "Default Password"
Dim authorization As New Authorization(Username, Password)
Dim myReturnClass as ReturnClass
myReturnClass = authorization.PromptUser()
MsgBox(myReturnClass.Username & " " & myReturnClass.Password)
于 2012-12-06T04:47:32.413 回答
1

您可以像任何其他类一样向该类添加属性以从外部对其进行修改:

Public Class Authorization

    Friend Property RefUsername As String
    Friend Property RefPassword As String

    'this constructor isn't really necessary with this new approach,
    'but I'll leave it as-is.
    Public Sub New(ByRef Username As String, ByRef Password As String)
        InitializeComponent()

        RefUsername = Username ' 
        RefPassword = Password
    End Sub

    Private Sub OKButton_Click(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles AuthorizeButton.Click

        Me.DialogResult = DialogResult.OK

    End Sub
End Class

然后从外部获取用户名和密码:

Dim Username As String = ""
Dim Password As String = ""

Dim authorization As New Authorization(Username, Password)
'this constructor isn't really necessary with this new approach,
'but I'll leave it as-is.

If authorization.ShowDialog() = DialogResult.OK Then
    MsgBox(authorization.RefUsername & " " & authorization.RefPassword)
End If
于 2012-12-06T04:53:47.663 回答