Solved

static variables mix between users

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
daypilot scheduler 3 30
IIS issues - Convert to Application - APS.NET v4.0 3 37
How come this regular expression allows "e"? 14 38
transaction in asp.net, sql server 6 37
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 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