Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 17th, 2007, 01:48 AM
Registered User
Join Date: May 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with static variables behaviour

I have made an asp.net application in which i have use static variables so i can use these values from one page to other

page or on the same page (on the same page bcoz if i use simple variables then on page postback these variables loss

their data).
Now I publish this application on local intranet. now suppose if i initialize this varibale say( myStaticVar )to 0 as follow

myStaticVar =0.

if there are two users that access this application and one user change its value to 1
myStaticVar =1
then the problem occurs that the second user also get the change value.This means the application is not thread safe. how

this problem can reslove to seperate each user's values to other user's data. as i have declare so many static variables in

my application.
Thanks in advance.

Old July 17th, 2007, 07:30 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

hi there.. you better store this data in cookies or session variables, to avoid passing data from one user to another...



================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
================================================== =========
I know that CVS was evil, and now i got the proof:
================================================== =========
Old July 18th, 2007, 11:09 AM
Authorized User
Join Date: Jul 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts

Static variables are independent of any particular object.
They are bound to classes & not objects.
dont use static variables,becuase what is happening is how static variabls behave
Old July 18th, 2007, 09:22 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

I don't think that "thread safe" is being used correctly here. Either way, whether the static var is or isn't thread safe is irrelevant.

Your web assemblies are loaded in one application, the ASP.NET worker process. So the static variables are in the scope of all page requests that come in.

You should really avoid using static variables. Instead you should be using any other means for user based values, which could be viewstate, session or anything else persistent (like a database).

Is there a reason you have chosen static vars instead of something like session?

Old July 21st, 2007, 01:56 AM
Authorized User
Join Date: Jan 2007
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aliirfan84

Dear u can use hidden labels(s) in order to store values. Static variables are shared variables and they keep thier last stored value as long as application is running state.

Ali Irafn

Similar Threads
Thread Thread Starter Forum Replies Last Post
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
Declaring static variables in ASP lakshmi_annayappa ASP.NET 1.0 and 1.1 Basics 1 May 24th, 2007 06:49 AM
Static Variables For HTML Header Parmy XSLT 1 February 20th, 2006 03:46 PM
who destroy static variables(no instance of class) MikoMax J2EE 1 March 31st, 2004 08:01 AM
A problem with static data members in c++ amotsn C++ Programming 2 August 20th, 2003 02:22 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.