473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Application State vs. "global" variables

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
4 3508
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
9
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
2
by: starbuck | last post by:
Hi My first vb.net/asp.net application is taking shape now and I have started using session state to store user variables or type string, long and bool.. The VB6 app I am converting also makes...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
6
by: Frank Swarbrick | last post by:
Interesting! I was going to ask if such a thing existed, but I was pretty much convinced they did not so I didn't ask. Looks like with version 9.5 DB2 supports global variables: "Global...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.