Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

static variables mix between users

Posted on 2006-11-25
5
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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 an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

718 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