473,384 Members | 1,854 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,384 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 1397
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;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.