How can I share some information from Form2 with Form1?
Would I make my variable like this?:
Public myVar As String
Or how is the best way of doing this?
Printable View
How can I share some information from Form2 with Form1?
Would I make my variable like this?:
Public myVar As String
Or how is the best way of doing this?
If you are displaying the second form from the first using ShowDialog then that is the easiest, and probably best way to do it, although you may want to think about a private variable and a public property.
e.g.It's not really necessary in a lot of cases but can be a better option in others.VB Code:
Private m_MyVar as String Public Property MyVar() As String Get Return Me.m_MyVar End Get Set(ByVal Value As String) Me.m_MyVar = Value End Set End Property
So do I make a private variable on Form2, and add the property to Form1 to grab the information, or does both go onto Form2 and I merely call m_MyVar to get the value from Form2 into Form1?
Cheers. :)
If you do want to use the property method they both go in Form2. Form1 then simply reads the value of the property from the Form2 variable.
Reading a property does take longer than reading a variable, so if you're concerned with getting the best performance then you may want to use the straight variable. Otherwise I'd suggest using properties always. Notice that there is almost no class in the .NET Framework that exposes straight variables, called Fields in the help. It is usually only for the odd Shared constant. Everything else is exposed through properties.
Edit:
The performance difference I speak of is miniscule, though. In fact I think it's actually nanoscule, or perhaps even picoscule.
OK, and stupid question...
How do I read a property? As I've never really used one before...
Cheers. :)
You use them all the time actually. Everthing you see in the Properties window in the designer is a property. If you were to use the code snippet I provided above, you could do something like this in Form1:VB Code:
Dim myDialogue As New Form2 If myDialogue.ShowDialog() = DialogResult.OK Then 'The user clicked the OK button. MessageBox.Show(myDialogue.MyVar) End If
Doh!
OK, thanks man. :)