Public Variables in VB.Net

KeysCoder

Member
Joined
Apr 10, 2006
Messages
18
Location
Florida Keys
Programming Experience
3-5
Hi everyone! This is my first post here. I'm yet another vb6 coder finaly taking the .net plunge. :eek: If I understand the OOP concept correctly, there really is no place for public variables in it. I know vb.net still allows it but it doesn't seem to fit the new paradigm.

In vb6, I often used public variables to store things like user preferences which needed to be visible to multiple procedures across the application. What's the proper way to handle that in .net? Put them in a structure? A class? How about adding them to the main application form as properties?
 
Public variables are important in vb.net as they ever were in vb. Declaring a variable public would expose it to the application it is running in and also outside of that application, but only to those that own an instance of that class.
As for your user preferences, in my experience there is no difinitive way to to do it as everyone has different ideas on how they prefer to accomplish it. But yes a public variable inside a class would suffice. I wouldn't personnaly use a structure for that, but that doesn't mean to say you cant. You could declare them in a module, but bear in mind that if you do they will automatically be marked as shared, which just means that you won't need an instance of that class to access them.
 
My personal preference is to add a module called 'PublicVariables' to the project.
All public variables are declared there and available throughout the project. Plus it is a 'one stop' location for all public variables.

P.S. Welcome to the forum!
 
That's exactly what I did in vb6. I've just gotten the impression from more experienced programmers that public variables should be avoided if at all possible. I know that they consume stack space. Other than that issue, is putting them in a class versus just declaring them public just a matter of style?
 
in .Net instead of declaring variables as Public (which is visible outside of the running application) you should declare them as Friend which means the variable can be reached anywhere inside that application and no further
 
Public Variables

I put all my PUBLIC variables in SubMain. It is the 1st one to wake up, stays awake, and last to sleep. Nowadays, I no longer have PUBLIC variables, I changed them all to FRIENDs. I also prefix all these variable with a "g" to distinguise them from all other variables.
 
I think that you're confusing "public" and "global". A public variable is just one that is visible outside the type it is a member of. In .NET applications you normally don't expose variables publicly, but rather use a private variable and a public property. I think what you're asking about is a global variable, i.e. one that is visible throughout the project. Diffreent people will tell you different things. Many people will use a module with public members. Others will use a MustInherit class with all Shared members, which amounts to the same thing as a module. Others will use a singleton class. As you're using VB 2005 you should take a look at application settings and the My.Settings object.
 
Yes, I was talking about globals.

Thank's for the tip about the my.setting object! I didn't know about that. Now we have a standard object for storing preferences and other settings.
 
One thing that you should note about application settings is that user-scoped settings are read/write and a different set is stored for each Windows user under their own Documents and Settings folder. Application-scoped settings are read-only though. This is done because if the current user does not have administrative privileges then they do not have the required permissions to write to the application config file, which is stored under the Program Files folder. If you want to edit application-scoped settings at run time you need to do so by loading the config file into an XmlDocument object and editing the nodes directly. You would obviously need to check that the user is an administrator before doing so.
VB.NET:
        If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
            Dim configDoc As New Xml.XmlDocument
            Dim configPath As String = Application.ExecutablePath & ".config"

            configDoc.Load(configPath)

            'Edit nodes here.

            configDoc.Save(configPath)
        Else
            MessageBox.Show("You must be an administrator to edit the application settings.")
        End If
Note that no changes you make to the config file will be reflected through My.Settings until you either restart the application or else call Reload, although that will also reload all other settings too. I find that the best way to manage application-scoped settings that I want to edit at run time to be through a purpose-built class. You would load all the application-scoped values from My.Settings into an instance of this class at startup, make any required changes to the proprties of this class along the way then use the method above to save the changes at shutdown or at any point along the way.
 
Hi this confused me a bit. To share variables throughout my whole application I've been using Public Shared or Public ReadOnly. I don't want these variables to be accessible by other applications though. Can you clear up the difference on public and private variables for me too please.
 
Ok here's the list...

Public - Available to all who own an instance of that class.
Friend - Available to the application in which it was dimensioned as long as an instance of that class is declared.
Protected -Available to classes that derive from that class.
ProtectedFriend - Add the above two together.
Private - available only to the class it was dimensioned in.

PublicShared - available to all whether an instance of that class is declared or not.
FriendShared - Avaliable only to the application it was delcared in whether an instance of that class is owned or not.
 
Ok so I want to be using FriendShared? My variables need to be passed between forms within the application, without class restrictions.
 
Back
Top