using variables

nitin

Member
Joined
Mar 29, 2005
Messages
9
Programming Experience
Beginner
hi people.pls help me out with this problem.

how to use a variable value used in one form in another form?like in asp we use querystring an retrive the variable value in the previous page how to access the same in VB.NET.
 
add a module to the project and in the module declare the variables as Friend varibale As type then in the forms you need to use the variable simply refer to the variable name

such as:

module:
Friend gstrCustFirstName as String

customer form:
gstrCustFirstName = txtFirstName.Text

order form:
lblCustFirstName.Text = gstrCustFirstName
 
Or, if the form you need to use the variable on is "related" to the form that contains the variable, you could get it through the Owner property. You'll have to make sure that when you call the forms's Show method, you pass the parameter for the owner.
VB.NET:
Form2.Show(Form1)
Then, in Form2 when you need the variable on Form1 you can get it like this:
VB.NET:
'Note, this is just a little pseudo-code to give you the idea.
Dim sForm2Value as String = CType(Me.Owner, Form1).Form1Variable

Or, as you were saying about using the QueryString in ASP, you could also overload the second form's constructor to take a parameter as well. This way, you would pass the value you need to the new instance of the form when you declare it. In the overloaded constructor (I prefer to overload the constructor to leave room for another developer to create a "default" or standard instance of the form without having to pass in the variable when they instanciate it but, that's just personal preference. You CAN simply modify the form's existing constructor), simply assign the value you passed in to a form level variable on Form2.
VB.NET:
Dim f As New Form2(yourValue)
f.Show
 
Last edited:
hi mothra. sorry to say but u r solution regarding using variables is not working.

Dim f As New Form2(yourValue)
f.Show


the first statement doesnot take the parameter .Also using statement

Form2.Show(Form1) it shows error sayin form1 is a type and cannot be used as an expression.



pls say if i m wrong in usin or is there any other problem
 
For the first statement to work you'll have to first change the constructor (or add an overloaded one) on Form2...

VB.NET:
[size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2] Form2
[/size][size=2][color=#0000ff]	 Inherits[/color][/size][size=2] System.Windows.Forms.Form
 
#[/size][size=2][color=#0000ff]Region[/color][/size][size=2] " Windows Form Designer generated code "
 
[/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][size=2][color=#0000ff]ByVal[/color][/size][size=2] whatever [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff][color=black][[/color]your Type[color=black]][/color][/color][/size][size=2])
[/size][size=2][color=#0000ff]		 MyBase[/color][/size][size=2].New()
 
[/size][size=2][color=#008000]	 'This call is required by the Windows Form Designer.
[/color][/size][size=2]	 InitializeComponent()
 
[/size][size=2][color=#008000]	 'Add any initialization after the InitializeComponent() call
 
[/color][/size][size=2][color=#008000]	 'Set the value of your Form2 variable to the value you passed in
	 [/color][/size][size=2]yourLocalVariable = whatever
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub[/color][/size]
 
[size=2][color=#0000ff][color=#008000]'...[/color]
 
[/color][/size]

As for the second, you are right. I was thinking of .ShowDialog() method which DOES take a paramerter to set the form's Owner (sorry about that :eek: ).

It isn't necessary to add an overloaded constructor to the form but, that is my personal preference. That way, if for some reason I want to create an instance of the form without passing in a value, I can. Hope that clears it up a little for you.
 
Last edited:
yes, mothra's solution does work, but is it really worth all that trouble when Friend Variables in a code module are global to the entire application (as i posted above)?
 
I was just offering annother alternative. That's all.
 
if i need to use a variable like an application variable in asp.should i use the same thing as suggested my juggalo.does the variable be updated from each form i use.
 
nitin said:
if i need to use a variable like an application variable in asp.should i use the same thing as suggested my juggalo.does the variable be updated from each form i use.

yes, there's only one variable for the whole application because it's global to the entire app if it's changed on any of the form's then it's changed and the other forms that use it will see the new value

think of it as a variable that's similar to a module level except multiple modules can access and use it
 
You can pass the value to the form by using parameters. Some academics nag when you use the module-and-friend method, saying that it is better practice to create a initialize() method for each form into which you pass all the required values through parameters.
 
bloukewer said:
You can pass the value to the form by using parameters. Some academics nag when you use the module-and-friend method, saying that it is better practice to create a initialize() method for each form into which you pass all the required values through parameters.

The reason for this is that using global (or application) level variables breaks encapsulation. They're thinking is that if too many things have asscess to something, there's more of a chance of it being corrupted. I think it really all comes down to preference. Sure, using a code mdoule to hold global variables may not be the "correct" way to code the solution to particular problem but, if it makes the application more difficult for you to read and maintain then, it may not be worth it.
 
i've actually had teachers who did it both ways, but i personally can code much faster by using a module and designating it with a g such as:

gstrCustFirstName = Global Level
mstrCustFirstName = Module Level
strCustFirstName = Subroutine Level

of course when i'm writing a class i dont allow the variables to be accessed directly, i do go through the trouble of making property procedures and an initialize method

but this is just me in case anyone was wondering
 
Sinning is easy

Yup. I also prefer to create a module which references to all of my forms - that way I can easily communicate between two forms and the code is also much easier to read. Using the parameters-method is a tedious way of doing things, not to mention the fact that it makes code difficult to understand for new (or even not-so-new) team members. This is especially true in cases where a form has a lot of variables that needs to be initialized. Imagine passing anything more than 5 variables into an initialize() method - Phew! As JB mentioned you should restrict access to the forms's variables by using property declarations.

One disadvantage of the module-method is that you often have one more file that has to be updated than with the parameter-method.

Most programmers, and it seems that this also holds true for seasoned programmers, prefer the module-method.

If your work is marked by a lecturer, however, I suggest you find out if it is permissable to do it this way.
 
Back
Top