pass value between forms in vb.net

tutu

Member
Joined
Apr 17, 2006
Messages
12
Location
india
Programming Experience
1-3
hi all,
in my vb.net application i have a dropdown list in form 1 and the value selected in this dropdownlist is used in the other forms of my application.how can i declare a variable so that its value is accessible to all other forms in my application.
 
Last edited:
global varialbe

one method of doing this is to add a module to your project and declare the variable in there. A safer method would be to pass the variable into the constructor when calling the form.
 
Declaring global variables is inherently unsafe and completely defeats the whole idea of encapsulation. You should pass in the value to your form instance, either through it's contructor or via a property or method.
 
alternately create a public property on the form...

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]ReadOnly[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] thing()
[/SIZE][SIZE=2][COLOR=#0000ff]  Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]    Return[/COLOR][/SIZE][SIZE=2] comboBox1.Text
[/SIZE][SIZE=2][COLOR=#0000ff]  End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

you either make Form1 aware of the other forms and have it give them the info..
or you make the other forms aware of form1 and have them ask it for the info.

of the two, i tend to choose the latter
 
Back
Top