473,396 Members | 2,109 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,396 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 3512
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.