473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Variable Lifetime

I created a simple asp.net web application, with a button.
I declared the var tmpStr
Imports ....
Public Class myClass
dim tmpStr as string

Private Sub Page_Load
check for postback and set tmpStr
if not ispostback then
tmpStr = "ABCDEFG"
'I debug check tmpstr here and it is "ABCDEFG"
end if
end sub

AND THE BUTTON CLICK..
private sub myButton_Click(...)
if tmpStr = "ABCDEFG" then do something..
look at the value of tmpStr and it is empty
end sub

Is it normal for tmpStr to loses its value after pageload. tmpstr is null
when I click the button. MSDN says var. retains its value(lifetime) until
app ends if not a local var.
Thanks KT
Nov 18 '05 #1
2 1398
Hi

You have to understand the sequence of these events
In the first loading of the page, Page_Load is run through, tmpStr is assigned to "ABCDEFG". Button has not been clicked, myButton_Click event is not triggered. Then page unload from server, tmpStr is destroyed
In the upcoming postbacks if myButton clicked, Page_Load is run through, however IsPostback is true, tmpStr is not assigned. myButton_Click event is triggered.
So if tmpStr is assigned to "ABCDEFG" without the condition of IsPostback, you will get the right value in myButton_Click event

Bin Song, MC

----- bucrepus wrote: ----

I created a simple asp.net web application, with a button
I declared the var tmpSt
Imports ...
Public Class myClas
dim tmpStr as strin

Private Sub Page_Loa
check for postback and set tmpSt
if not ispostback the
tmpStr = "ABCDEFG
'I debug check tmpstr here and it is "ABCDEFG
end i
end su

AND THE BUTTON CLICK.
private sub myButton_Click(...
if tmpStr = "ABCDEFG" then do something.
look at the value of tmpStr and it is empt
end su

Is it normal for tmpStr to loses its value after pageload. tmpstr is nul
when I click the button. MSDN says var. retains its value(lifetime) unti
app ends if not a local var
Thanks K

Nov 18 '05 #2
a class variable has the same life of its class. it is created and destoryed
when the class is created or destoryed.

on render asp.net create a new version of your page class, calls the events
then releases. when the postback happens the same thing happens as they are
seperate page requests.

-- bruce (sqlwork.com)

"bucrepus" <bu******@hotmail.com> wrote in message
news:uO*************@TK2MSFTNGP12.phx.gbl...
I created a simple asp.net web application, with a button.
I declared the var tmpStr
Imports ....
Public Class myClass
dim tmpStr as string

Private Sub Page_Load
check for postback and set tmpStr
if not ispostback then
tmpStr = "ABCDEFG"
'I debug check tmpstr here and it is "ABCDEFG"
end if
end sub

AND THE BUTTON CLICK..
private sub myButton_Click(...)
if tmpStr = "ABCDEFG" then do something..
look at the value of tmpStr and it is empty
end sub

Is it normal for tmpStr to loses its value after pageload. tmpstr is null
when I click the button. MSDN says var. retains its value(lifetime) until
app ends if not a local var.
Thanks KT

Nov 18 '05 #3

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

Similar topics

7
by: lallous | last post by:
Hello, Why is the static variable inside a member function static for all instances and not static for the current instance of the object? -- Elias
7
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've...
11
by: Trapulo | last post by:
I've a question about globa variables lifetime in an asp.net app. I've declared this class: Public Class Utils Private Shared _FcdDataManagement As FCD.DataManagement Public Shared...
6
by: junw2000 | last post by:
When I define a static variable, where is the memory allocated for the static variable? Thanks. Jack
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.