'Form 1
Public myVariable as String
Private Sub LoadForm2()
Dim frm as New Form2
frm.Owner = Me
frm.Show
End Sub
'Form 2
Private Sub myTest()
Dim frm as Form1 = Me.Owner
Msgbox(frm.myVariable)
End Sub
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] _MyIndex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] MyIndex() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] _MyIndex[/SIZE][/INDENT]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] value [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#008000]'To show the interest of using a property vs a variable[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] value > 0 [/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] value < 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/INDENT][INDENT][INDENT][SIZE=2]_MyIndex = value[/SIZE][/INDENT]
[/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][/INDENT][INDENT][INDENT][SIZE=2][COLOR=#0000ff]Throw[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Exception([/SIZE][SIZE=2][COLOR=#a31515]"Invalid Index"[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT]
[/INDENT][INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Shared[/COLOR][/SIZE][SIZE=2] _Instances[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Shared[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReadOnly[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] Instances() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] _Instances[/SIZE][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]()[/SIZE][INDENT][SIZE=2]InitializeComponent()[/SIZE][/INDENT][INDENT][SIZE=2]_Instances += 1[/SIZE][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_FormClosed([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.FormClosedEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].FormClosed[/SIZE][INDENT][SIZE=2]_Instances -= 1[/SIZE][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Private myVar As String
Friend Property VarProperty As String
Get
Return myVar
End Get
Set (ByVal value As String)
myVar = Value
End Set
End Property
'Module1:
Friend myVar As String
'Form1:
myVar = "123"
'Form2:
Me.TextBox1.Text = myVar