|
From: JIM.H. on 14 Feb 2005 10:57 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.
From: Scott Allen on 14 Feb 2005 11:28 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. -- Scott http://www.OdeToCode.com/blogs/scott/ On Mon, 14 Feb 2005 07:57:05 -0800, JIM.H. <JIMH(a)discussions.microsoft.com> wrote: >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.
From: Ranv on 14 Feb 2005 16:10 JIM.H. <JIMH(a)discussions.microsoft.com> wrote in message news:<B8329FFE-0BC6-41A9-9B86-8A6A4510A8BD(a)microsoft.com>... > 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.
|
Pages: 1 Prev: DateTime Format Next: Null Reference Exception When Using Sesssion State |