0

フォームから派生したクラスがあり、ユーザー名とパスワードのテキストボックスと [OK] ボタンがあります。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

要するに、ユーザーが [OK] をクリックしたときにクラス外の変数の値を変更したいのですが、どうすればそれを達成できますか?

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 に答える