0

I am currently developping an app WinForm using vb.net and i have to call a function, situated in my main form, in another form. Here is my function in my main form

Public Sub ImprimCalibrage(t1 As String, t2 As String)
    Dim strCalibr As String = Build_str("CAL", "CALIBRAGE", t1, t2)
    BuildFile("fileT.txt", strCalibr)
    Print()
End Sub

To call it, i have created an attribute in my secondary form like this ReadOnly form1 = Application.OpenForms("Main")

And now i call my function doing this form1.ImprimCalibrage(TextBox1.Text, TextBox2.Text)

But when i click on the button which supposed excute the function, i got this error

'Object variable or With block variable not set.'

I don't have any idea how to solve it because i dont have with block and my only variable in just a String.

Poupou
  • 3
  • 1
  • 3
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Apr 29 '21 at 08:04
  • When code stops like his, normally visual studio opens with a tooltip pop up thing pointing at the exact location of the problem. Show a screenshot of that thing – Caius Jard Apr 29 '21 at 08:04
  • It is practically certain that `Application.OpenForms("Main")` is returning a `Nothing` reference. You have to make sure the form has been created and shown before you execute that statement. See proposed duplicate for additional details about debugging and fixing this type of exception. – Peter Duniho Apr 29 '21 at 08:06
  • @CaiusJard i don't know how to send screenshot here... – Poupou Apr 29 '21 at 08:16
  • @PeterDuniho, in fact, i used a reference like this in another form and it works so i don't know. And i think my form is created because when i run my app, it start with the main form – Poupou Apr 29 '21 at 08:16
  • _"i think my form is created"_ -- then you need to prove it. Post a proper [mcve] that reproduces the problem, while demonstrating that it happens even though the form you're trying to retrieve has in fact already been shown. – Peter Duniho Apr 29 '21 at 08:18
  • *"i have to call a function, situated in my main form, in another form"*. No you don't. The proper way to do this is for the other form to raise an event that the main form handles and then the main form calls its own method. The other form shouldn't even know that the main for exists. – jmcilhinney Apr 29 '21 at 08:30
  • That said, VB supports default form instances to make it easier for beginners and VB6 developers to do the wrong thing easily and without breaking things. If, by main form, you mean your startup form then it is already the default instance of its type, which means that you can access it anywhere via the default instance. For example, if `Form1` is your startup form then, from anywhere else in the app, you can just call `Form1.SomeMethod()` and it will work. The default instance is always accessed via the type name. There's no need to do anything else. – jmcilhinney Apr 29 '21 at 08:33
  • @jmcilhinney , thank you i didn't know i could do that because when i tried i forgot to declare my form as a variable object. – Poupou Apr 29 '21 at 08:55
  • *i don't know how to send screenshot here* - open snipping tool, snip the shot, press ctrl-c, edit your question, press ctrl v, an imgur url appears - can either submit it as part of the question or copy it and paste it into a comment. Do not solely use screenshots of code; prefer text, just use screenshots when asked (we'll know when we want the extra context) – Caius Jard Apr 29 '21 at 09:43

1 Answers1

0

Application.OpenForms("Main") is returning Nothing because at the time it is called (when Form2 is created) , there is no open form named Main in the collection. This Nothing then persists until you try and use it. You can't call a method on Nothing. Even if you later ensure there is a form named Main in the OpenForms collection, it doesn't alter the fact that at the time you looked (and captured) there was nothing

Move the code to just before you need it

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim form1 As Form1 = Application.OpenForms("Main")
    form1.ImprimCalibrage(TextBox1.Text, TextBox2.Text)
End Sub
Caius Jard
  • 72,509
  • 5
  • 49
  • 80