Accessing variables declared in Global.asax.vb

N

Nathan Sokalski

I would like to access variables and functions that I declare in the
Global.asax.vb file. However, I am having trouble doing that. What does the
declaration have to look like in the Global.asax.vb file, and what would I
do to access it? (I am using VB.NET for my code) Thanks.
 
J

Jc Morin

Hi,

Global.asax is a class that is always called before your page is loaded, but
it's not part of of your page really. (Master page in asp.net 2.0 will be
more what you are looking for)

The only thing I know you can access from global.asax are Public Shared
(Static in c#) functions or variables.

Using shared variable must be considered very dangerous because they are
often source of non-thread safe result. If you want to carry information
between page, better look into Session variable.

Hope that help you,
 
K

Kevin Spencer

Hi Nathan,

A file is code. It does not have anything to do with the program, other than
the fact that the code in it is compiled into one. The global.asax file is
all code. It will help if you understand this, as well as what it creates
when compiled, which is what you need to be concerned about.

The global.asax file is a code file that defines a class. This class is used
at run-time to fire events that occur during the lifetime of your app. The
name of the class is "global" - not "global.asax," as "global.asax" is a
file name. I hope I'm getting this across clearly. The class is used by the
ASP.Net worker process during its lifetime, that is, the lifetime of the
ASP.Net application, which lives from the moment the first Request to your
application occurs, until 20 minutes (default) after the last Request
occurs, or whenever the web server is restarted, or, on a Windows 2003
server, whenever the Application Pool for your ASP.net app is recycled.

It contains a number of event handlers, which are pre-wired by the ASP.Net
ISAPI, to be fired at certain times during the lifetime of your ASP.Net app.
Each of these event handlers is a method (a function, if you will). Any
variables declared inside a method are scoped locally to that method. That
is, they do not exist when the method exits, just as they would in any other
class method.

I'm just trying to give you some background here, so that you will
understand better, and hopefully not encounter this situation again in some
different form.

So, as to the answer, let's start by forgetting that these variables and
functions are declared in the global.asax file. They are members of a class.
Yes, the class is named "global" but that doesn't mean that everything in it
IS global. However, these event handers give you an opportunity to CREATE
some global variables. The global class has access to the Application Cache,
which lives for the entire lifetime of your application, and is, in fact,
accessible to all pages and classes used in your application. In addition,
several event handlers in the global class have access to user-specific
"global" cache, that is Session state. If you want to persist something in
state that is accessible only to one user client, and only for the time for
which that user client is connected, store it there.

Of course there are other state mechanisms that have nothing to do with the
global class, or global event handlers, such as ViewState, which is
Page-specific.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
Joined
Oct 17, 2006
Messages
2
Reaction score
0
More details needed

This wasn't my question, but it looked like it would answer my question. It didn't, so maybe someone can add additional clarification.

Kevin Spencer said:
A file is code. It does not have anything to do with the program, other than the fact that the code in it is compiled into one. The global.asax file is all code. It will help if you understand this, as well as what it creates when compiled, which is what you need to be concerned about.

The global.asax file is a code file that defines a class. This class is used
at run-time to fire events that occur during the lifetime of your app. The
name of the class is "global" - not "global.asax," as "global.asax" is a
file name. I hope I'm getting this across clearly.

Right, I understand that, of course.
The question I have is how to access the Global object.
I obviously don't want to instantiate a Global object, since that should automatically be created by the application. I suspected at first that perhaps the global "Application" object would give access, but no.

So, from a page object, how do I access the global "Global" object in order to call methods in it?

-Worlebird
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top