VB.NET Global Variables

BorisMatthews used Ask the Experts™
I am porting am application from Access to VB.NET and am considering the best practice for storing 'global' variables for strings, integers etc and even forms.

I have done some research and can see that it is possible to create a standard module in vb.net to store the global variables (as used in Access & VB6).  Is this an acceptable method or are there any reasons why this should not be used and if so how should this be done?

Looking at forms, I have a few forms that are never closed and should only ever have one instance.  I have seen that this can be accomplished by creating a singletion class member for the form and refering to this class.  However, I have also tried simply adding "Public frmMyForm as New frmMyForm" into the standard global variables module and this emmes to work fine, I simply refernce this from the other forms and classes in my project.  Is there a reason why this is not good practice as it seems a very simple way of achieving what I need!.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Maybe this will help

or this
There is no one answer to the question. All the methods you listed should 'work.' Which you should use depends why you want to share the variable. For example:

1.Say you have a form with a list of records, and the user double-clicks a record, so you want to open a new form to edit the record, and you want to pass the record ID. In this case I would add a constructor method to the second form: Sub New(RecordID as String) 'Add code to load the record here End Sub

2.Say some of the forms in your application may want to know the database path or something else global like that. For that, I would make the appropriate variable on the parent form into a Public variable (called a Field) and access it as MainForm.FieldName. (Disclaimer: Purists will say you shouldn't rely on the somewhat messy fact that VB.NET automatically instantiates an instance of the form class and lets you refer to it by the form name, and that you should instead get a pointer to the actual instance of the form and store it in your child form and access the parent form like that. Actually, this is like number '2' in your post. But it's not actually necessary if you don't mind programmatical incorrectness.)

3.Say there is something global in your app, like the time the app was started, so you can tell the user "You've been using the app for 5 hours, go get a life!" These things could be stored in a module. (Or in the application class but that's quite hidden)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
"I have a few forms that are never closed and should only ever have one instance."

For VB.Net 2005 (and above) you can just use the default instance of the forms (instead of the singleton approach).  Just use the forms name, without the new keyword:

   Form2.xxx = yyy

This can be done from anywhere.
If you want to store connection strings and some static values, you can use an app.config file and access it from anywhere in your application.


Thanks to both responses, they each answered one part of my question each.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial