English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How can I declare a variable in VB.net which can be used in ALL OF THE FORMS?

2006-10-11 22:41:37 · 4 answers · asked by Amir M 2 in Computers & Internet Programming & Design

4 answers

You can do this by using "Public Shared" Declaration.
(The variable will save its value between forms)

Example:
Suppose you have two forms, Form1 with name "Form1", and Form2 with name "Form2".
Go to Form1, and outside any procedure, declare the variable "TestVar" as follows:
Public Shared TestVar As Integer = 67

In this way you can access TestVar from any procedure in Form1 and from any form that belongs to the same project.
To access the variable TestVar from Form2:
in a procedure in Form2 write:
MsgBox("TestVar = " & Form1.TestVar)

The Message Box result must be "TestVar = 67"
*************************************
Note also that AllSeeingEye's Answer is important, you can make class in the project, and declare variables with "Public Shared" declaration (in our example "TestVar"), then in any form you can write:
MsgBox("TestVar = " & Class1.TestVar).
or:
Dim c1 As New Class1
MsgBox("TestVar = " & C1.TestVar).

2006-10-12 09:12:42 · answer #1 · answered by SK 1 · 0 0

I don't know VB very well, but in most real languages that would be called a "global variable". Variables defined inside objects or functions are local to that object or function. If you want a variable to be global, you'll probably have to define it in the same place that you define the forms themselves. I don't know how to do this in VB, and I care even less, but I'm sure if you check the help files it will all be in there.

Rawlyn.

2006-10-11 22:54:07 · answer #2 · answered by Anonymous · 0 0

Create a module and include that inside your solution. inside this module, create your global variable and this can be used inside all of your forms. The other way of doing it is of course creating a class that has shared properties and instantiate this class in each of the forms you wish to set/recieve the global value in. This would be a more object oriented way of creating global variables.

Hope this helps

2006-10-12 01:16:20 · answer #3 · answered by jools 3 · 0 0

dim a1 as integer
where dim is a dimension

2006-10-11 22:48:09 · answer #4 · answered by ch_nagarajind 3 · 0 1

fedest.com, questions and answers