我想知道如何设置我通过引用传递的变量的值。在这个例子中,我传递一个整数,但在应用程序中,它可能是一个不同的对象。
无论如何,这里有一个简短的总结:
代码示例:
Option Strict On
Public Class SpecialForm
Inherits System.Windows.Forms.Form
Private WithEvents _Btn As System.Windows.Forms.Button = Nothing
Private _Result As Integer
Public Sub New(ByRef Out_Result As Integer)
_Result = Out_Result
End Sub
Private Sub SpecialForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
_Btn = New System.Windows.Forms.Button
With _Btn
'Set size
'Set location
'Set text
'etc
End With
Me.Controls.Add(_Btn)
End Sub
Private Sub Btn_Click(sender As Object, e As System.EventArgs) Handles _Btn.Click
_Result = 5555
Me.Close()
End Sub
End Class
Module EntryPoint
Public Sub Main()
Dim result As Integer
Using mySpecialForm As New SpecialForm(result)
mySpecialForm.ShowDialog()
End Using
MsgBox(String.Format("The result is {0}", result)) 'DEBUG MESSAGE
End Sub
End Module
也许我做错了,或者说这可能是语言的限制。但有没有办法在不使用全局的情况下做到这一点?
答案 0 :(得分:2)
语句_Result = Out_Result
存储Out_Result的值而不是其地址。
您应该使用类的实例,例如:
Class MyData
public value as integer
End Class
你可以这样做:
_Result.value = 5555