473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class session variables

I finally got class session variables to work by putting the following in
global.asax in the session_start:
dim myDBComp as DBComp = new DBComp........
session("myDBCo mp") = myDBComp

In each aspx codebehind module I can define my object right after the class
definition as so:
dim myDBComp as DBComp

The problem I had was where to put the instantiation:
myDBComp = CType(session(" myDBComp"), DBComp)

I can't put it up with the definition as the compiler wants only
declarations up there.
I can't put it in any of the methods because it loses scope outside the
methods.
I finally found that it works if I put in in initializecompo nent()

But then it disappears if I make any changed to the UI in the aspx page.

Where is it supposed to go?
Thanks,
Dean
Nov 17 '05 #1
4 1666
The instantiation will last the object while the page object exists. Putting
it in page_init or page_load, will mean this variable is set until the page
finishes executing.

"Dean" <de*********@ea rthlink.net> wrote in message
news:ea******** ******@TK2MSFTN GP10.phx.gbl...
I finally got class session variables to work by putting the following in
global.asax in the session_start:
dim myDBComp as DBComp = new DBComp........
session("myDBCo mp") = myDBComp

In each aspx codebehind module I can define my object right after the class definition as so:
dim myDBComp as DBComp

The problem I had was where to put the instantiation:
myDBComp = CType(session(" myDBComp"), DBComp)

I can't put it up with the definition as the compiler wants only
declarations up there.
I can't put it in any of the methods because it loses scope outside the
methods.
I finally found that it works if I put in in initializecompo nent()

But then it disappears if I make any changed to the UI in the aspx page.

Where is it supposed to go?
Thanks,
Dean

Nov 17 '05 #2
Marina:
According to a test that I just ran, it went out of scope as soon as the
page_load event ended. Page_init is part of the "web form designer
generated code", in fact initializecompo nent() is called from there so
manually entered code gets clobbered there just like code in initialize
component.

So, unless I have made a mistake in my test, question still stands.
thanks,
Dean

"Marina" <mz*******@hotm ail.com> wrote in message
news:ui******** ******@tk2msftn gp13.phx.gbl...
The instantiation will last the object while the page object exists. Putting it in page_init or page_load, will mean this variable is set until the page finishes executing.

"Dean" <de*********@ea rthlink.net> wrote in message
news:ea******** ******@TK2MSFTN GP10.phx.gbl...
I finally got class session variables to work by putting the following in global.asax in the session_start:
dim myDBComp as DBComp = new DBComp........
session("myDBCo mp") = myDBComp

In each aspx codebehind module I can define my object right after the

class
definition as so:
dim myDBComp as DBComp

The problem I had was where to put the instantiation:
myDBComp = CType(session(" myDBComp"), DBComp)

I can't put it up with the definition as the compiler wants only
declarations up there.
I can't put it in any of the methods because it loses scope outside the
methods.
I finally found that it works if I put in in initializecompo nent()

But then it disappears if I make any changed to the UI in the aspx page.

Where is it supposed to go?
Thanks,
Dean


Nov 17 '05 #3
If the variable is declared at the class level, and initialized in page_init
or page_load, it will stick around for the lifetime of the page - which is
just until it finishes executing and when the response is sent to the
browser.

If you are declaring the variable inside a method - then it will have only
the scope of the method. This is why it has to be a class level variable.
After that, you can initialize it where ever you want - and it will stay
around for the lifetime of the object.

I don't know what test you did, but this is how it works. You can put code
into page_init - initializecompo nent is meant for designer generated code.
page_init is yours to customize, those people usually use page_load.
"Dean" <de*********@ea rthlink.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Marina:
According to a test that I just ran, it went out of scope as soon as the
page_load event ended. Page_init is part of the "web form designer
generated code", in fact initializecompo nent() is called from there so
manually entered code gets clobbered there just like code in initialize
component.

So, unless I have made a mistake in my test, question still stands.
thanks,
Dean

"Marina" <mz*******@hotm ail.com> wrote in message
news:ui******** ******@tk2msftn gp13.phx.gbl...
The instantiation will last the object while the page object exists.

Putting
it in page_init or page_load, will mean this variable is set until the

page
finishes executing.

"Dean" <de*********@ea rthlink.net> wrote in message
news:ea******** ******@TK2MSFTN GP10.phx.gbl...
I finally got class session variables to work by putting the following in global.asax in the session_start:
dim myDBComp as DBComp = new DBComp........
session("myDBCo mp") = myDBComp

In each aspx codebehind module I can define my object right after the

class
definition as so:
dim myDBComp as DBComp

The problem I had was where to put the instantiation:
myDBComp = CType(session(" myDBComp"), DBComp)

I can't put it up with the definition as the compiler wants only
declarations up there.
I can't put it in any of the methods because it loses scope outside the methods.
I finally found that it works if I put in in initializecompo nent()

But then it disappears if I make any changed to the UI in the aspx page.
Where is it supposed to go?
Thanks,
Dean



Nov 17 '05 #4
There is deffinitely a difference in the life of a component depending if it
is initialized in Page_init or Page load:
I first declare my component at the class level of my page as follows:
Dim myDBProc As PhotoDBProcs

I then initialize it in the page_load as follows:

myDBProc = CType(Session(" myDBProc"), PhotoDBProcs)

I use the component to build content and, at the end of the page_load it
response is sent to the browser.

One of the buttons gets pushed triggering an event on the page. In the
button event the component no longer exists.

When I initialize this component in InitializeCompo nent() (or page_init),
however, the component still exists when the button is pushed.

Your first paragraph on your response is technicaly true but that is not
sufficient for any real application that has many interactions on a page.

So, to get the necessary scope of life on this component, I need to
initialize it in InitializeCompo nent() and re insert it each time it gets
wiped out upon UI change.

"Marina" <mz*******@hotm ail.com> wrote in message
news:Ok******** ******@TK2MSFTN GP12.phx.gbl...
If the variable is declared at the class level, and initialized in page_init or page_load, it will stick around for the lifetime of the page - which is
just until it finishes executing and when the response is sent to the
browser.

If you are declaring the variable inside a method - then it will have only
the scope of the method. This is why it has to be a class level variable.
After that, you can initialize it where ever you want - and it will stay
around for the lifetime of the object.

I don't know what test you did, but this is how it works. You can put code into page_init - initializecompo nent is meant for designer generated code.
page_init is yours to customize, those people usually use page_load.
"Dean" <de*********@ea rthlink.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Marina:
According to a test that I just ran, it went out of scope as soon as the
page_load event ended. Page_init is part of the "web form designer
generated code", in fact initializecompo nent() is called from there so
manually entered code gets clobbered there just like code in initialize
component.

So, unless I have made a mistake in my test, question still stands.
thanks,
Dean

"Marina" <mz*******@hotm ail.com> wrote in message
news:ui******** ******@tk2msftn gp13.phx.gbl...
The instantiation will last the object while the page object exists.

Putting
it in page_init or page_load, will mean this variable is set until the

page
finishes executing.

"Dean" <de*********@ea rthlink.net> wrote in message
news:ea******** ******@TK2MSFTN GP10.phx.gbl...
> I finally got class session variables to work by putting the following
in
> global.asax in the session_start:
> dim myDBComp as DBComp = new DBComp........
> session("myDBCo mp") = myDBComp
>
> In each aspx codebehind module I can define my object right after
the class
> definition as so:
> dim myDBComp as DBComp
>
> The problem I had was where to put the instantiation:
> myDBComp = CType(session(" myDBComp"), DBComp)
>
> I can't put it up with the definition as the compiler wants only
> declarations up there.
> I can't put it in any of the methods because it loses scope outside

the > methods.
> I finally found that it works if I put in in initializecompo nent()
>
> But then it disappears if I make any changed to the UI in the aspx page. >
> Where is it supposed to go?
> Thanks,
> Dean
>
>



Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1769
by: Earl Teigrob | last post by:
I am programming ASP.NET using C#. I have been accessing static variables accross my entire application but now need to change some of the static variables that are session specific to instance variables. (For more background, see previous post about 30 min ago) It was so cool using static variables because they where global to the...
1
4846
by: Qingdong Z. | last post by:
Posted in asp.net newsgroup, no answer. Hope some experts can help me here 1. Does class shared variable share same feathers as ASP.NET application variable? 2. If it is, it may work better than application variables, because we don't need type conversion 3. Can web farm share same class shared variable? This is not critical for my case,...
2
2278
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 In the current release of our system, we decided to "wrap" the ASP.NET Session and Application objects to improve code clarity and accuracy. For...
1
2628
by: Beren | last post by:
Hello With trial and error I'm attempting to create an extended identity to store some more data than just the Name, for example a Subscription and a LastSearchPerformed property... Is this a good idea ? I'm coming from ASP and Session variables, but I explicitly wanted to avoid that for .NET. The problem I'm facing is that I don't...
1
3752
by: aliendolphin | last post by:
I'm having a strange problem possibly with Session variables for one user being accessed by another. I use a static utility class with static methods which get and set certain Session variables. For Example: public static string UserEmail { get { return HttpContext.Current.Session; } set { HttpContext.Current.Session = value; }
7
1373
by: fisab | last post by:
I apologise in advance for such a basic question, but I'm hoping someone will take the time to answer my question. In my code I define a Dataset : Partial Class Default_aspx Dim dsExcel As New DataSet I fill the Dataset as follows :
12
7294
by: Thomas Andersson | last post by:
Hi, How can I access a session variable within a Public Class? I have tried the below code, but I get a server error "Object reference not set to an instance of an object". System.Web.HttpContext.Current.Session.Add("myKey", "myValue") Dim a As String a = System.Web.HttpContext.Current.Session.Item("myKey")
15
2399
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with 10 items it might use 1KB, and if I load it with 1000 items it might use 100KB. How do I measure the amount of memory used once the class is loaded? Thanks! -- Joe Fallon
4
4215
by: Diffident | last post by:
Hi All, I am trying to perform a non-CPU bound operation in an asynchronous fashion using 'Thread' and a delegate. Basically, I am spawning a thread using ThreadStart and Thread. My non-CPU bound operation needs to have access to Session variables; Even though I embedded the state information (which also includes the context object) in...
0
5000
by: bharathreddy | last post by:
Here I will given an example on how to access the session, application and querystring variables in an .cs class file. Using System.Web.HttpContext class. 1) For accesing session variables : System.Web.HttpContext.Current.Session 2) For accesing Application variables : System.Web.HttpContext.Current.Application 3) For accesing QueryString...
0
7504
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6026
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.