Hi,
How I can declare global variables in vb.net that I can access them in all windows of my application?
Hi,
How I can declare global variables in vb.net that I can access them in all windows of my application?
Last edited by almaha; 08-16-2010 at 6:39 PM.
You can use modules or even better classes (need to be instantiated as an object before use).
If you go for classes with shared members you don't have to instantiate them before use.
If i was you i would probably go for modules as they are more easier to be used.![]()
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.
As lordofduct suggests, global variables should generally be avoided. If you explain why you want this global variable then we can explain the best option.
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.
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.Originally Posted by almaha
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.
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly
Bookmarks