Is there a way to pass values ​​between forms in vb.net? Without using the public variable

I have an application that uses a second form with a textBox and a button, I need to transfer text from textBox to the first form. I know how to do this with the public variable, but I would like to know if there is another way how to do this without using a public variable.

+4
source share
2 answers

You can use the PUBLIC properties in the second form to read / write information to another form.

You can overload the NEW method to pass the variable during the declaration.

You can create a public sub / function function in the second form and pass a byval or byref variable.

But I would never use a public variable. This is bad programming.

+1
source

Yes!

If the forms are part of the same solution, you simply write:

Dim MyVal1 As Integer = Form2.MyVal 

MyVal1 exists in any form in which you write it, and MyVal2 - from form 2. These variables can only be used if the forms have the correct privacy settings.

If the two forms are part of separate applications, the only way I know to pass variables around is through constructors.

EDIT:

In your case with a text box, you can apply a similar solution:

 Dim val As String = Form2.TextBox1.Text 
+2
source

All Articles