Q: global variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
What is the life span of public variable defined in the class.
Is there any way to define a global variable in aps.net, and use it in all
the following pages.
Thanks,
Jim.
 
What is the life span of public variable defined in the class.
One page building one response, unless it is static.
Is there any way to define a global variable in aps.net, and use it in all
the following pages.
There is a number of ways. Session variables for the scope of a session.
Application cache, static variables for the scope of application. There are
also some other ways.

Eliyahu
 
The life span of a variable is as long as the class instance it is
associated with, and the object will live for as long as you hold a
reference to the object. We typically don't hold a reference to a Page
object since they are only used to process a single request. You could
put an object reference inside the Session, Cache, or Application
objects to keep the object around a bit longer.

You can also mark a field as static (shared in VB.NET) and the field
will be around for the duration of the application. A static field
also means there is only one instance of the field in the entire
application - no matter how many objects get created - so you have to
be sure this is what your design really calls for. Globals like this
are a double edged sword.
 
The life span of any variable is the life span of the object in which it is
declared. A public variable defined in a class is part of the class, and
lives for the life of the class.

However, don't make any assumptions yet. A Page class lives from the time
the Page is requested until the time the Response is sent. Not long at all.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
Subject: Re: global variable
From: "Kevin Spencer" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet

The life span of any variable is the life span of the object in which it is
declared. A public variable defined in a class is part of the class, and
lives for the life of the class.

However, don't make any assumptions yet. A Page class lives from the time
the Page is requested until the time the Response is sent. Not long at all.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.


Jim,

In an OOP language like C#, you have many options as to variable
category and the scoping. Note the URLs below wrap.

See

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/csspec/html/vclrfcsharpspec_5_1.asp

and

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/csspec/html/vclrfcsharpspec_3_7.asp


The best way to implement a "global" variable is through the use of a
static class.

-- ipgrunt
 
JIM.H. said:
Hello,
What is the life span of public variable defined in the class.
Is there any way to define a global variable in aps.net, and use it in all
the following pages.
Thanks,
Jim.

Trying using the Session object. For instance, lets say you want to
store a user name that you need for all your pages, the code would
look like this: (I'm using C#)

Session.Contents["username"] = "jim";

and then in another page, to retrieve this information, you would:

string user;
user = Session["username"].ToString();

That's pretty much it, the name of the array index "username" is also
up to you, you can call it whatever you want. Check out MSDN for a
further explanation of the session object. Hope that helps.
 

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

Back
Top