By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Application State vs. "global" variables

P: n/a
BB
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
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
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

Nov 18 '05 #2

P: n/a
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/
----------------------------------
Making waves on the Web
"Muckey" <no***********@spamispoo.com> wrote in message
news:2uVYb.3611$Ru5.2712@okepread03...
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" <an*******@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
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


Nov 18 '05 #3

P: n/a
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.
Regards,

Bill Borg

Thanks,

Bill Borg

Nov 18 '05 #4

P: n/a
"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" <an*******@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
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

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.