Shared (static) members and global variables

Every time I try to explain shared (static) members I end up in a discussion over global variables. Usually the discussion goes like this : “Hey I just heard there are no global variables in .NET and now I don’t see the differnece between MyGlobalvar and MyCalss.GlobalVar”.

I have several answers :

  • A shared member has identity. It is member of typeX.
  • The shared members does not exist without the type is a member of
  • Public types are “global” to your application. And also to any code referencing the class.
  • Not every type is public
  • Not every shared member is public

These don’t convince everybody. Does anybody know of a simpler way of explaining the difference ?


This entry was posted in Coding. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • pvanooijen

    Create a class with internal scope, make the desired varaibles memebers with internal scope.

    No example no c#-> This should be enough information to get going and
    still have the feeling to actually know what you’re doing :)

  • VbDEV

    I am developing an application in I am new to this and I appreciate your help.

    I need to declare few variables to be used /modified from various classes and forms within a project.

    I do not want these variable to be available out side the project. What is the best way of doing this? any help with example is appreciated. Please provide any possible example in . Thanks.

  • pvanooijen

    The Module keyword is a relic of ancient VB. In C# it would be just another class
    public class Globals {

    public string g_strPIN = “”;

  • Ron Howerton

    It is indeed possible to create global variables in VB.NET:

    Public Module Globals
    Public g_strPIN As String = “”
    End Module

    Whether this is desirable is another matter. Globals behave like one would expect in a Windows application. In the ASP world, however, they are treated like SHARED variables. What one user sets another sees – not usually a desired effect.

    Wish this was better documented!

  • pvanooijen

    @ Andy,

    When you have a public global in a public class, like

    class MyGlobals
    static public int MyA;

    all of your code can use MyGlobals.MyA. The class prefix alway has to be included. In case your class has another MyA member there are two vars. MyA and MyGlobals.MyA. These are totally independent, the only thing they share is a part of their name.

  • Andy

    So what do you call a variable that can be read and updated absolutely anywhere in the whole project, which can’t be masked by any kind of local dimming?

    Is there a way to declare a variable (as whatever you call the above) so that it can’t subsequently be used for any other purpose in the program, project ? i.e. if you delcare a (as whatever you call the above) then a is always a and can’t be dim’d to anything else or some local a means something different ?

  • pvanooijen

    Yup, static is C# syntax, shared

  • ITGalary

    so shared and static means the same na??????

  • Peter van 0oijen

    OK, I can do that :) But I don’t think I want to do it :> Most people are perfectly happy without their globals. Or they can live with them as shared memebers. The quote is from a "new purist".

  • Jason Bock

    Technically, you can add global variables outside of a class definition at the CIL level. Check out Section 9.8 of Partition II for details. True, this isn’t something that’s allowed in C# or VB .NET (for good reason) but you can still do it :).



  • Peter van 0oijen

    hello Dan,

    that’s a nice tutorial. It mentions global vars as one of the many uses of shared members. As it is. I think we should stop saying "VB.NET has no global vars", but should say "Global vars are coded different (OOP-ish) in VB.NET"

    Modules ? Never heard of them :>

  • Dan

    Hello Peter,

    You should make them read this before trying to teach them :

    – Shared is only global to the class that the member is part of (since it can be private too).

    – The rest of the application may use it, but don’t own it.

    – This is best explained with Properties, since the owner adds validation to the Get and Let.

    – This variable would be available without an instance of the class.

    – This variable would be shared by all instances of the class.

    VB.NET is messing it all up with the use of Module, but you probably shouldn’t tell them about it.

    Am i on track ?