Best way to use Global variable

B

bnob

I have to keep a variable (integer) during the navigation of my asp.net
project (vb.net code behind).

So actually I use a module where I declare a global variable

Public MyGlobalVariable as integer

In all my form I refer to this variable with MyGlobalVariable .

Is this method the best way to use global variable ? Or is session
variable better ?
 
J

John Saunders

bnob said:
I have to keep a variable (integer) during the navigation of my asp.net
project (vb.net code behind).

So actually I use a module where I declare a global variable

Public MyGlobalVariable as integer

In all my form I refer to this variable with MyGlobalVariable .

Is this method the best way to use global variable ? Or is session
variable better ?

Your global variable is global to every user who accesses your application.
If you wanted all of your users to access this same variable, all at the
same time, with no synchronization, then you're doing it the right way.

But you probably wanted one instance of your variable per user, in which
case you should use session state.

There is almost no reason to ever use a Module. In fact, the most important
use of a Module is to identify yourself as a VB programmer. ;-) All of the
other uses of a Module are less important than that.


John Saunders
 
J

Jeff Evans

bnob said:
I have to keep a variable (integer) during the navigation of my asp.net
project (vb.net code behind).
So actually I use a module where I declare a global variable
Public MyGlobalVariable as integer
In all my form I refer to this variable with MyGlobalVariable .
Is this method the best way to use global variable ? Or is session
variable better ?

If you don't need it persisted across requests, and it is to be unique for
each client, you should store it in

HttpContext.Current.Items

and it will be available to every page throughout the request. Otherwise
use HttpContext.Current.Session to store it in the session.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top