0

I am a Windows Forms guy moving into web development. On all my other programs I would create a static class to hold program wide variables such as

public static class GlobalVariables
{
public static DateTime expDate;
public static DateTime quoteDate; 
public static decimal discountTotal1 = 0;
public static decimal discountTotal2 = 0;
public static decimal discountTotal3 = 0;
public static decimal finalTotal;
public static decimal netWeight;
public static decimal qlWeight;
public static decimal runningTotal;
public static decimal workingTotal;
}

A friend of mine who is a professional web developer said that all those variables would be getting overwritten when multiple users use my web application.

Is he correct? If so why? How do I make these variables user specific?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
CryptoJones
  • 734
  • 3
  • 11
  • 33
  • Note that static variable is bad practice in WinForms too, but it is possible to live with it if you don't try to test your code or have multiple instances of anything (since you can control the environment unlike web sites case). – Alexei Levenkov Jun 11 '14 at 04:49

1 Answers1

2

In web application use Session variables if you want them globally. For example: Session["discountTotal1"] = 0; These session variables are user specific.

static variable scope is application wide and will be shared among all users. you need to use Session to store per user, so it is accessible in all pages. On the other hand, if you just need it on a specific page, you can save it in ViewState and can get it in post back.

Ricky
  • 2,323
  • 6
  • 22
  • 22
  • Note that Sessions are ultimately tied to a user's browser - so, if a user opens multiple "tabs" in their browser, they'll all be sharing the same Session and you could have undesired behavior depending upon what you use that data for. C.f. http://stackoverflow.com/questions/3967205/asp-net-session-id-shared-amongst-browser-tabs – JimMSDN Jun 11 '14 at 20:15