Solved

static variables mix between users

Posted on 2006-11-25
5
220 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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