Solved

static variables mix between users

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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