0

I just want to create a simple counter which increases its value when a button is pressed in the current session. So, I'm trying to make an int variable in global.asax in the

void Session_Start(object sender, EventArgs e)

which has an initial value of 0, obviously. In addition to that, I will set its value to 0 again when the session ends. Since variables made in global.asax have global visibility, why does it throw an error when I try to refer to it at my button's event page?
I'm creating it as

public static int p1;

This is the line of code I use it in the other page:

p2 = p1 + counter;
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • What kind of error are you getting? – Josh H Jan 22 '18 at 18:16
  • "The name 'p1' does not exist in the current context." (error hits at the page where the button is, in the line I try to increase it) –  Jan 22 '18 at 18:17
  • a line of code will say more than a thousand words. – Cee McSharpface Jan 22 '18 at 18:20
  • Thie line of code I'm using it in is: p2 = p1 + counter; –  Jan 22 '18 at 18:21
  • 1
    Do not describe your code. Add it directly to question. Read more about [code examples](https://stackoverflow.com/help/mcve). By the way - title of your question contains `VB` but question is tagged as `c#`. So what is your code language c# or visual basic? – Aleks Andreev Jan 22 '18 at 18:26
  • Just updated the question and tags, thanks for that. I must've added c# as an accident. –  Jan 22 '18 at 18:29
  • @Konstantina I've edited a question, but still not sure about `vb.net` tag. Add it if you really use it. BTW `visual studio` tag is irrelevan - your question have nothing to do with this IDE - same code could be created with notepad – Aleks Andreev Jan 22 '18 at 18:40
  • Thanks again! I'm a newbie and thought it was relevant. –  Jan 22 '18 at 18:44

1 Answers1

0

You need to access p1 from the class it is declared in in global.asax. For example:

public class MvcApplication: HttpApplication {
   public static int p1;

   protected void Application_Start() {
      ...
   }
}

Increment by:

MvcApplication.p1 = MvcApplication.p1 + counter
Josh H
  • 264
  • 1
  • 10
  • A public static variable is not safe to use in ASP.NET and will likely fail at some point. See [this answer](https://stackoverflow.com/a/8919336/215552) for more information. – Heretic Monkey Jan 22 '18 at 18:43
  • Thanks, Josh, it seems to work. However, I was looking for a simpler answer, since we're still at the beginning of the class so the `MvcApplication: HttpApplication` part doesn't make much sense to me haha. But I will look more into that in the future for sure. –  Jan 22 '18 at 18:49
  • Mike has a good point. That this is not safe in the long run. Another possibility could be to create a static class as a singleton containing a counter that you could increment. – Josh H Jan 22 '18 at 19:02
  • there is a storage mechanism in ASP.NET for each scope (request, context.cache, session, application). Application-scope variables normally go into [this one](https://stackoverflow.com/a/5548906/1132334). – Cee McSharpface Jan 22 '18 at 19:27