global variables

almaha

Member
Joined
Aug 11, 2010
Messages
7
Programming Experience
Beginner
Hi,
How I can declare global variables in vb.net that I can access them in all windows of my application?
 
Last edited:
be warned, global variables can lead to some very bad design choices. Be very careful with them.

I use them for very few things... notably I use them for:

Constants
Logging methods
Utility Methods (as Shared/Static member classes)... like a Math class or something.
 
l have some variables initialized in one window and need to be check from another windows , that's why i need global variables
 
That depends on context, but usually you pass the information on through properties or as method arguments, going with the program flow.
almaha said:
l have some variables initialized in one window and need to be check from another windows
This doesn't really doesn't explain much about the context, or the program flow. For example, if one window is shown as a dialog from another, then you can pass info to dialog before shown and retrieve info from dialog when it returns. As independent windows, if one shows another it may pass the info at that time if available. If the info is not available at that time you may pass the form reference so that the other window can retrieve the info when and if it may become available. If using single instance forms you can also in such scenario use the default form instance reference to access the other window.

As for using global variables (in modules) I'd say that would be as convenience in application scope where the data is single instance, and the state of the data is not relevant in any context; ie there would not be a dependency about what is put there, who puts it there, when it is put there, and when it is retrieved.
 
Back
Top