Global variables...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone plz tell me how i could declare a global variable that is available throughout all my forms in my application?


I am new to vb.net and need advise how to do this!
 
Varun said:
Can anyone plz tell me how i could declare a global variable that is
available throughout all my forms in my application??
I am new to vb.net and need advise how to do this!

Presuming you mean throughout a single multi-form project, look up "shared"
in the help. You can declare a variable as public shared (outside any code
block), and it will be available to other code within your project. E.g.
Public shared myInt as Integer

From the help:
"The Shared keyword indicates that one or more declared programming elements
are shared. Shared elements are not associated with a specific instance of a
class or structure. You can access them by qualifying them either with the
class or structure name, or with the variable name of a specific instance of
the class or structure.

The Shared keyword is used in these contexts:

Dim Statement

Event Statement

Function Statement

Property Statement

Sub Statement "


There are other options which may be preferable, but this will get you the
access you seek.
 
Stefano said:
do you know which is the C# equivalent syntax?

"static" is (at least pretty much, if not entirely) the C# equivalent
of VB.NET's "Shared"
 
Stefano said:
Hi Peter,
do you know which is the C# equivalent syntax?
Thank You

Presuming you mean in a multi-form environment, the easiest way is to
declare a public static variable in one class within the solution namespace,
then refer to it from other classes using the class name qualifier.
Given namespace Fudd containing classes Form 1 and Form 2:
In class Form1, declare: public static int myInt;
Code in class form 2 can access myInt using: Form1.myInt

Hope this helps,
 
Back
Top