0

i want to store data lets say ID in a centralized area that i can access it from any class, statics won't help because it will be overwritten, i need something like modules in vb (i don't know if it really helps me) how can i do this in asp/C#?

Hilmi
  • 3,411
  • 6
  • 27
  • 55
  • What is the persistence strategy for your data? If it's "per session", then you could use the Session object. If it's unknown then a database is the obvious choice. If it's time-based then use Cache and expiry policies. – Strillo Mar 27 '12 at 08:11

3 Answers3

3

You could use:

Not knowing exactly what it is you want to centralise (and the scope of it) I cannot offer advice on what option to pick. Most of the time, Session state tends to be used.

You've won the first battle by opting not to use statics, they are unpredictable on IIS due to Application Pool recycling.

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
0

you may store it on Session variable like;

Session["myParam"] = myobject;

you may retrieve value like;

var temp = (MyClass) Session["myParam"];
daryal
  • 14,643
  • 4
  • 38
  • 54
0

If you are in a web context so you can use Application object because this is a common object for all the classes and pages.

So add your value to application then read it once needed from the other location.

you can as well get the same behavior if you define a static class with static variable inside. So take a look to singletone design pattern to get more details for this solution

http://www.dofactory.com/Patterns/PatternSingleton.aspx

sherbeny
  • 11
  • 3