Where to put global variables

  • Thread starter Thread starter michael
  • Start date Start date
M

michael

From an experienced developers view point ... just don't use global
variables. It can cause major havoc. But to answer your question, you
could simply put then in a file (for example: global_vars.asp) of it's own
and then do the following when needed:

<!--#include virtual="global_vars.asp"-->

Hope this helps
 
Where is the best place to put global variables. In traditional ASP I used
to put all of them into an include file and include it in every page. Will
the Global.aspx.cs do that same thing?

Thanks in Advance!
Andrea
 
Depends really.....
I use the web.config for some things, beyond that I may use a base form that
has all the others inherited from it.....
I'd say it really will depend on what types of things you are storing and
how often you request them.
Session objects/items can work quite well also.
 
I would typically use the application object, or the appsettings section of
web.config.

You need to be very careful with them though, make sure you take care if you
plan to update them realtime as they are obviously shared.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
You could load them into the application object from web.configs setting
section at app startup via the global.asax files events. Changing
web.config would automatically reload app startup, so you are not
technically changing your app to change the values.

You can turn your global.asax file into a class. I've attached a small
example below, although it updates a global counter you should get the
idea.....

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

<%@ Application Classname="MyGlobals" %>

<script language="VB" runat="server">

public shared UserCount as integer = 1

Sub Application_OnStart()
' Application startup code goes here.
End Sub

Sub Session_OnStart()
' Session startup code goes here.
Application.Lock()
Application("SomeGlobalCounter") =
CType(Application("SomeGlobalCounter"),Integer) + 1
Application.UnLock()
End Sub

Sub Session_OnEnd()
' Session cleanup code goes here.
End Sub

Sub Application_OnEnd()
' Application cleanup code goes here.
End Sub

</script>
 
Thanks you to all of you! I have more questions.

Ok, so let's say that I put them into the web.config and I want to create
wrapper that gets them and enable the code to select them as properties.
Would it be ok to use the Global.asax.cs to make these available to
everything. I'm used to using modules in VB and C# doesn't seem to have
that ability. Should I be doing htis in a different way?

Main thing is, if they are setup constants, I want to be able to change them
without having to send my recompiled dll's oto the server. And I want to
gain access to them from any code-behind pages in my app.

I have tried to set up a class in a separate dll, but I'm unable to access
the propeties on the dll from my ASP code. They are set as public so I'm
not sure why I can't access them.

Andrea
 
Hello John Timney and Andrea Williams,

I also have the same problem with Andrea Williams.
My designing goal is that:

1. The global values can be changed,
2. if they are changed, they can be stored back,
3. Retrieving them would not yield too much load to the server
(quick and easy to fetch)
4. Altering them won't have the application restart.
5. Variables have default values.

Thus,
1. Storing values in Web.Config is not a feasible way since
modifying that file will restart the application.
2. Storing values in Global.asax is not feasible too, since the
changed values can not be saved between application restarts.
3. Storing values in an external database is expensive to load
values.

Regarding this, i designed a model:
1. Variable values are stored in an external database;
2. On Application_start, the appication will attempt to load values
into the global variable, the ApplicationState Items, from the
database (if the loading process failed, i set a WebSiteSuspended
global variable to True, the following request will continue to
reload from the database still, and set WebSiteSuspended to
False if the database is ready and values are loaded)
3. On Application_end, the application will attempt to save all
global variable back to the database, from the ApplicationState
Items.
4. There's also viable to store values back to the database during
the runtime of the application, periodically.
5. Default values are hard coded in the Global.asax. If there's no
corresponding value in the database, the default value is applied.
after loading the values, to ensure that the following application
pages, controls can directly get the value from
Application["var_name"] without having to determine whether
the value exists.

How do you think about this model? Any suggestions, please!

Jordan
 
Back
Top