Solved

static variables mix between users

Posted on 2006-11-25
5
210 Views
Last Modified: 2007-01-12
I have a problem that I solved but I don't understand the reason and I want to understand.
I have a site in ASP.NET 2.0 and C#.
I have a page that include static variables in the begining of this page.
In the page_load I put values to the static fields.
I have a button in the page "close button" and when the user press the button , a function is
called 'savetohistory' with the static field.
I am adding the code but only what is needed for the problem:

public partial class MailOutlook : System.Web.UI.Page
{
    static int Cand_ID;
     static int UserID;

    protected void Page_Load(object sender, EventArgs e)
    {
         
        if (!IsPostBack)
        {  // At the beginning we read the input parameters if exists:
            string Cand_ID_S = Request.QueryString["Candidate_id"];
            Cand_ID = Int32.Parse(Cand_ID_S);

            string UserIDS = (string)Session["User_id"];
            UserID = Int32.Parse(UserIDS);
        }
 
    protected void btnClose_Click(object sender, EventArgs e)
    {
        EmailTemplates.saveToHistory(Cand_ID, UserID,Page);
        CloseScreen();
    }
}

The problem:
If many users enter this page at the same time, pressing the btnclose mix the values od the static fields UserID
and Cand_ID. If for example there were 3 users entering the page one after the other, the first and the second
users will get the values of the third and last user entered.
I solved the problem by changing the static fields into hidden fields in the page.
But was is the problem with static fields in a page?
Thankyou
Anat
0
Comment
Question by:ANAT2403
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18010985
Static variables are in scope for all static and instance methods of the defining class and classes that inherit the defining class.
In other words, the static variable is not instance specific, and will have the same value across all instances of the class. As you change it in one insance, you actually change it in the other as well. This is about as close as you can come to a 'global' in C#.

To quote from http://www.samspublishing.com/articles/article.asp?p=29400&rl=1.
"By creating static methods, you create methods that can be accessed from anywhere in the project through the class."

Some good articles there.
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18198048
Hi ANAT2403

Did my answer assist you? If not, please can you provide some feedback so that I can assist further. Alternatively, please have a look at http://www.experts-exchange.com/help.jsp#hs5 for information on closing questions.

Regards
Nightman
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now