0

I tried to set a global variable with a livetime during one pagerequest.

In classic als i used this like this:


dim VariableName VariableName = "test"; sub testsub() VariableName += VariableName + "new" response.write VariableName end sub response.write VariableName '-> test testsub() '-> testnew 

Now in asp.net i tryed to set the variable in my class like this:

public static class MyClass { public static string GlobalVar = "test"; public static string MyMethod() { GlobalVar += GlobalVar + "new"; return GlobalVar; } } 

But now, the problem is, that this variable are like a application variable with a lifetime over all pagerequest.

Where can i define a varible with a lifetime during one request and availiable in all methods and other classes?

1
  • This has bad design written all over it Commented Dec 28, 2009 at 12:53

3 Answers 3

5
HttpContext.Current.Items["ThisVariableHasRequestScope"] = "SomethingFancy"; 

Edit:

A simple example

AClass.cs :

public class AClass { public void Something() { // Set the value HttpContext.Current.Items["Test"] = "xxx"; } } 

BClass.cs

public class BClass{ public void SomethingElse() { // Get the value var test = HttpContext.Current.Items["Test"] as string; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I want to set this variable in MyClass and not in MyMethod(). Because I have to use this global variable in other files and classes, too. How I have to set this variable?
2

Try using ASP.NET Session and see if it fits yours needs.

Comments

0

You can also to use Page.Context property as it will be available during all page lifecycle.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.