Обновить значение переменной вне класса в VB.NET
У меня есть класс, полученный из формы, он имеет текстовое поле для имени пользователя и пароля и кнопку ОК. Я хочу, чтобы он вел себя как 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
Короче говоря, я хочу изменить значение переменных вне класса, когда пользователь нажимает кнопку ОК, как я собираюсь это сделать?
2 ответа
Решение
Вы можете просто добавить свойства в класс, чтобы изменить его извне, как и любой другой класс:
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
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)