CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

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 ?

Peter


Published Oct 18 2004, 03:01 PM by pvanooijen
Filed under:

Comments

Dan said:

Hello Peter,

You should make them read this before trying to teach them :
http://tutorials.beginners.co.uk/read/id/190

- 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 ?
# October 18, 2004 3:53 PM

Peter van 0oijen said:

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 :>
# October 19, 2004 3:05 AM

Jason Bock said:

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 :).

Regards,

Jason
# October 19, 2004 9:38 AM

Peter van 0oijen said:

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".
# October 20, 2004 2:56 AM

ITGalary said:

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

# September 18, 2007 3:54 AM

pvanooijen said:

Yup, static is C# syntax, shared vb.net

# September 18, 2007 4:22 AM

Andy said:

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 ?

# November 14, 2007 12:34 PM

pvanooijen said:

@ 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.

# November 14, 2007 3:39 PM

Ron Howerton said:

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!

# January 4, 2008 10:10 AM

pvanooijen said:

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

public class Globals {

   public string g_strPIN = "";

}

# January 21, 2008 7:57 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!