问题
I'm trying to find some help in the ways of "Initializing" A module/Variable inside the module on an event.
I need it to load, in order for the following WinForm to use it (apparently) Let me explain...
I am using the WinSCP library and gotten plenty of help in the ways of WinSCP by the author himself. But in order to fix my current problem, I need to Initialize a Global Variable. So the Variable is there, its in order, but "Form2" refuses to use it, as it apparently needs to be Started/Initialized from an event in Form1. Module is called Public Module Module 1.
Public Module Module1
Public mySession As Session
End Module
I need to Launch/Start it on this Event;
Private Sub Loginbutton_Click(sender As Object, e As EventArgs) Handles Loginbutton.Click
WinSCP Author was unable to help as it was outside WinSCP alone, and just .Net/VB issue. I've gotten so much excellent help here on Stackoverflow so I know someone here will be able to help.
It might be some easy thing I have overlooked and forgotten. My head is hurting at this time, so any help, code samples and other forms of help is greatly appreciated as my head is slightly hurting of all this Visual Studio stuff.
回答1:
You do this
Public Module Module1
Public mySession As New Session
End Module
Or
Public Module Module1
Public mySession As New Session
Sub New()
'//===>This code will execute when the application starts
mySession = New Session()
End Sub
End Module
Or create the instance on the button click event
Private Sub Loginbutton_Click(sender As Object, e As EventArgs) Handles Loginbutton.Click
mySession = New Session()
End Sub
if you can't none of this option then you need to read the documentation of the library, because sometimes require a parameter to initialize the object, or even you can not create a instance of it, because can only create the instance calling other function that will created inside (This kind of thing is for security reason)
来源:https://stackoverflow.com/questions/28769066/initialize-global-variable-net