Application State vs. "global" variables

G

Guest

Hello all,

I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away when it quits. I presume that one difference is that the application state can be "flushed," such as with on-line changes to global.asax, but any additional thoughts are appreciated.

Regards,

Bill Borg

Thanks,

Bill Borg
 
M

Muckey

public myVar as string

Scope: the page it is declared in. Loses value on postbacks. The public
qualifier does not really imply state at all. What public means is that any
class that references the class that holds myVar has access to that
variable. to illustrate:

Public class MyClass

Public myVar as string = "hello world"
Private myvar2 as string = "good-bye!"

end class

Public class _Default

dim oMyClass as new MyClass


dim test as string = oMyClass.myvar ' this works fine since myVar is public
it is accesible to _Default

dim test2 as string = oMyClass.myvar2 'This will throw an error because
myVar2 is only accessible inside MyClass

end class

to make a variable Global to the whole app you have 2 choices:

1. Session("MyID")

The session object will retain state past postbacks and pages. Session is
user specific (i.e. not shared) So my Session("MyID") will have a completely
different value from your Session("MyID"). You would use this for things
like passing a user id or the contents of a shopping cart throughout the
pages on your site the user accesses

2. Application("CountUsers")

the application object has almost all the same properties as session
however, the application object is available to all users. A good example of
Application("whatever") is at forums that show how many users are currently
signed in. that is to say that Application("CountUsers") will have the same
value for everybody.

There are tons of resources available on line that go into much further
detail if you need


BB said:
Hello all,

I might be missing something here, but am trying to understand the
difference between using application-level variables--i.e.
Application("MyVar")--and global variables--i.e. public myVar as string,
etc. It seems to me that the scope and duration are the same, as they both
are there while the application is running, and both go away when it quits.
I presume that one difference is that the application state can be
"flushed," such as with on-line changes to global.asax, but any additional
thoughts are appreciated.
 
R

Rick Strahl [MVP]

There are tons of state options in ASP.Net and knowing which one works best
is pretty important.

I personally prefer using public static properties to hold 'global' values
that are mostly read only and only change under controlled conditions since
that is the most efficient way to access values or objects. In that state no
persistence to an object store takes place and no colleciton lookup occurs.

Application provides the same functionality but has more overhead.
Application also automatically synchronizes access to values so you don't
have to worry about locking resources when writing values as you have to do
with statics (if there's potential for simultaneous updates).

Application is global to all users. For mostly read only values you can also
use the Cache object which is nice because you can also control the life
time of an item in the store and when it should be refreshed.

Session is per user.


+++ Rick ---


--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
 
H

Harsh Thakur

Hi,

Another point you might want to keep in mind is that your
Application and static variables will be reset if your
worker process (aspnet_wp.exe) recycles due to some reason.

Session variables, on the other hand, will maintain their
values even if the worker process is recycled. Of course,
one assumes that you have NOT chosen "InProc" as the mode
for the session.

HTH
Regards
Harsh Thakur
-----Original Message-----
Hello all,

I might be missing something here, but am trying to
understand the difference between using application-level
variables--i.e. Application("MyVar")--and global variables-
-i.e. public myVar as string, etc. It seems to me that
the scope and duration are the same, as they both are
there while the application is running, and both go away
when it quits. I presume that one difference is that the
application state can be "flushed," such as with on-line
changes to global.asax, but any additional thoughts are
appreciated.
 
K

Kevin Spencer

"Public myVar As String" is a declaration of a public field in a class. This
is not what one would refer to as a "global" variable unless one were
talking about class scope. In other words, the field is global to the class,
and is accessible to any code that uses that class. An Application variable
is a variable that is stored in a Collection in the Application class. The
Application class is accessible to all pages and users within the scope of
an ASP.Net web application. It is, therefore, truly global.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

BB said:
Hello all,

I might be missing something here, but am trying to understand the
difference between using application-level variables--i.e.
Application("MyVar")--and global variables--i.e. public myVar as string,
etc. It seems to me that the scope and duration are the same, as they both
are there while the application is running, and both go away when it quits.
I presume that one difference is that the application state can be
"flushed," such as with on-line changes to global.asax, but any additional
thoughts are appreciated.
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top