Getting a varible on another form

devil666

Member
Joined
Dec 2, 2006
Messages
8
Programming Experience
1-3
If I make a vriable on frm1 for example, how can i access it on frm2?
 
You would need to make it public first of all, then you need to send the instance of Form1 to Form2 so it can access it.

When you create an Instance of Form2 you can set it's Owner property to Form1. This way you can reference it later. Now, there may be a better way of accomplishing this that someone else can mention.

Then reference the form.<variable>.

VB.NET:
'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
 
I am not so sure it's a good idea to put a variable Public to access it from another instance of of Form. I understood that properties where designed for that purpose, allowing you to control that the value you are passing is set correctly :

VB.NET:
[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]
 
Good call.

But just so I know, what is the negative effect of accessing a public variable as opposed to a public property accessing the private variable? Is it just improper coding, or is there a negative effect? I personally don't know this one.

Thanks,
 
When you use a property you can control that the value accessed from another object will be set to the range you want, you can also decide that it will be read only to avoid changes. For example you can create a read only property that will keep track of the number of running instances of your form:
VB.NET:
[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]
 
Bob,

I definitely see the advantages of using the properties. I was just wondering what the disadvantages of using a public variable were.

Thank you,
 
there isn't a disadvantage of using a public variable, but instead of using the 'Public' keyword i would use 'Friend' instead, this is for both application wide variables and properties

IE when you add a property to your form i would use:
VB.NET:
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
or:
VB.NET:
'Module1:
Friend myVar As String

'Form1:
myVar = "123"

'Form2:
Me.TextBox1.Text = myVar
 
Back
Top