Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Since "static" variables are shared by all instances of a class, I was wondering if they're shared through out all open pages of the website?

For example: Let's say I have the following:

 private static string SelectedName = "";

 protected void SendButton_OnClick(object sender, EventArgs e)
 {
     SelectedName = NameTextBox.Text;
     DisplayLabel.Text = SelectedName;
 }

 // ... other functions that depend on SelectedName's value

If two people open my page.. User #1 types in "Bob" in the name text box and hits send. User #2, in some other location, now opens the page. Will they see "Bob" as the DisplayLabel's text?

share|improve this question
1  
Have you not tried it yourself? – Arran Jul 19 '13 at 16:27
    
I did and it looks like the answer is no, but I didn't want to make any assumptions. I was also hoping for some sort of explanation, since it goes against what I've been taught that static variables are shared throughout all instances. – Kirsten Koa Jul 19 '13 at 16:31
1  
@cskoala There are matters that complicate the issue, such as whether or not you have multiple web front ends, and of course in a cloud environment you almost certainly will. In a cloud environment you probably shouldn't be using static variables at all. If you need to share state between users there are constructs that ASP (or your could provider) provides specifically for that. – Servy Jul 19 '13 at 16:35
    
Thanks for an answer, @Servy! I'm not trying to share state between users.. at this point, I'm actually trying to prevent it. The reason why I used static variables in the first place is that I noticed that nonstatic variables aren't persistent throughout the whole life time of the opened page. It seems like a new Default.aspx.cs instance gets created whenever any reaction methods are triggered. After doing this, I realized I might run into problems when multiple people are using the page at once (which is very likely to happen). – Kirsten Koa Jul 19 '13 at 16:46
1  
@cskoala if you want the state to be specific to a user's session there a number of means, ranging from using view state, to session, to a database, and any number of other options. static variables aren't what you want here. – Servy Jul 19 '13 at 16:47

Static variables have application scope. Meaning that users can share data through statics, but only in a single worker process on a single server.

share|improve this answer
    
Since the app is hosted by the cloud the application won't have a single worker process on a single server, so the statement doesn't hold and it doesn't have an application scope. – Servy Jul 19 '13 at 18:01
    
That is what I said right? "but only in a single worker process on a single server" – Kees de Wit Jul 19 '13 at 18:09
1  
Yes, you did say that, but since it's not true, and the previous statement doesn't apply, it leaves the answer with nothing. The answer isn't wrong, it just isn't helpful since the information doesn't apply to the OP's case. – Servy Jul 19 '13 at 18:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.