-1

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.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
PiperMp3
  • 53
  • 9
  • what do you mean "'Form2' refuses to use it" ? As shown `mySession` is simply declared not instanced. FWIW, it probably need not be `Public`, `Friend` would likely be enough. – Ňɏssa Pøngjǣrdenlarp Feb 27 '15 at 15:58
  • "Instanced"! Is the word, thank you. Well, once form2 is loaded, it needs to use information from the library WinSCP. And you can check out the post here; http://stackoverflow.com/questions/28745899/load-save-documents-to-sftp-linux-server-with-vb-net Check the last 3 comments. It gives me errors during debugging. "For Each i As RemoteFileInfo In mySession.ListDirectory("/Database").Files" - 'Object reference not set to an instance of an object.' – PiperMp3 Feb 27 '15 at 16:03
  • Nearly all NullReference Exceptions have the same set of causes. See [NullReference Exception in Visual Basic](http://stackoverflow.com/a/26761773/1070452) for help on this. This is specifically covered in the answer. – Ňɏssa Pøngjǣrdenlarp Feb 27 '15 at 16:04
  • @PiperMp3: From what you say, it sounds as though in your Loginbutton.Click handler you need something like `mySession = New Session(args)`. I can't tell you what `args` needs to be, that depends on what the constructor for the Session class expects. – Blackwood Feb 27 '15 at 16:09
  • Yep, it really was that simple. All about that "New" operator which I tried so many other versions of it. New this, new that, but they way I tried it was "New Session = mySession" and "New mySession = Session" which obviously was wrong. – PiperMp3 Feb 27 '15 at 16:18

1 Answers1

0

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)

MrAlex6204
  • 167
  • 7
  • Aaaah, I knew Stackoverflow is the way to go! I knew that "New" thing but I had no idea how to formulate that for Visual Studio. So I got a new reaction from using the code 'mySession = New Session()' And instead of NullReference Exceptions, I am getting 'Session not opened' which is probably more WinSCP related? – PiperMp3 Feb 27 '15 at 16:15
  • Yes, probably you need to pass some parameter when you create the instance Like: session = New Session("My UserName","Password") you can pass different parameters but that depends on how was the library developed. you can see more info of the lib. in visual studio with the intelligence type this first session = New Session( after this press keys Ctrl +Space bar and you will see the documentation of this function of how to create the instance. – MrAlex6204 Feb 27 '15 at 16:23
  • Oh, amazing, thanks a lot MrAlex. Faith in Stackoverflow maintained ;D Consider this case closed. – PiperMp3 Feb 27 '15 at 16:25