Peter's Gekko

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted Mon, Oct 18 2004 3:01 PM by pvanooijen
Filed under:

[Advertisement]

Comments

Dan wrote re: Shared (static) members and global variables
on Mon, Oct 18 2004 3:53 PM
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 ?
Peter van 0oijen wrote re: Shared (static) members and global variables
on Tue, Oct 19 2004 3:05 AM
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 :>
Jason Bock wrote re: Shared (static) members and global variables
on Tue, Oct 19 2004 9:38 AM
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
Peter van 0oijen wrote re: Shared (static) members and global variables
on Wed, Oct 20 2004 2:56 AM
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".
ITGalary wrote re: Shared (static) members and global variables
on Tue, Sep 18 2007 3:54 AM

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

pvanooijen wrote re: Shared (static) members and global variables
on Tue, Sep 18 2007 4:22 AM

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

Andy wrote re: Shared (static) members and global variables
on Wed, Nov 14 2007 12:34 PM

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 wrote re: Shared (static) members and global variables
on Wed, Nov 14 2007 3:39 PM

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

Ron Howerton wrote re: Shared (static) members and global variables
on Fri, Jan 4 2008 10:10 AM

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 wrote re: Shared (static) members and global variables
on Mon, Jan 21 2008 7:57 AM

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

public class Globals {

   public string g_strPIN = "";

}

VbDEV wrote re: Shared (static) members and global variables
on Wed, Jun 18 2008 4:13 PM

I am developing an application in vb.net. 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 vb.net . Thanks.

pvanooijen wrote re: Shared (static) members and global variables
on Sat, Jun 28 2008 6:46 AM

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

No example no c#->vb.net. This should be enough information to get going and

still have the feeling to actually know what you're doing :)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us