Session like variables
-
Hey, I need to share variables among forms in the Web session (or application) manner. I used to have some code - which I don't have now- that implements the singleton design pattern and allows adding and retrieving objetcs this way :
Application["MyObject"] = object;
Can you help? Thanks. -
Hey, I need to share variables among forms in the Web session (or application) manner. I used to have some code - which I don't have now- that implements the singleton design pattern and allows adding and retrieving objetcs this way :
Application["MyObject"] = object;
Can you help? Thanks.I would use a static class with a static member with an indexer:
public static class SC
{
private static SettingsClass settings = new SettingsClass();
public static SettingsClass Application
{
get { return settings; }
}
public class SettingsClass
{
Dictionary<string, object> settings = new Dictionary<string,object>();
public object this[string id]
{
get
{
return settings[id];
}
set
{
if (!settings.ContainsKey(id))
settings.Add(id, value);
else
settings[id] = value;
}
}
}
}add use it this way:
SC.Application["key"] = someObject;
Hope it helps anyhow.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Hey, I need to share variables among forms in the Web session (or application) manner. I used to have some code - which I don't have now- that implements the singleton design pattern and allows adding and retrieving objetcs this way :
Application["MyObject"] = object;
Can you help? Thanks.You can also use cookies, whose default expiration time is exactly one session.
Request.Cookies.Add(...); // add cookie
Request.Cookies[id]... // access cookieGreetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Hey, I need to share variables among forms in the Web session (or application) manner. I used to have some code - which I don't have now- that implements the singleton design pattern and allows adding and retrieving objetcs this way :
Application["MyObject"] = object;
Can you help? Thanks.Have a look at the
HttpContext.Current
object. You can access your session variables through it, and create some wrapper code around your session variables. Like this:using System.Web;
public static class SessionHelper
{
public static string SomeString
{
get { return HttpContext.Current.Session["SomeString"] as string; }
set { HttpContext.Current.Session["SomeString"] = value; }
}public static int SomeNumber { get { return (int)HttpContext.Current.Session\["SomeNumber"\]; } set { HttpContext.Current.Session\["SomeNumber"\] = value; } }
}
-
Hey, I need to share variables among forms in the Web session (or application) manner. I used to have some code - which I don't have now- that implements the singleton design pattern and allows adding and retrieving objetcs this way :
Application["MyObject"] = object;
Can you help? Thanks.Why have you posted a question on Web Forms in the Win Forms section?
Deja View - the feeling that you've seen this post before.
-
Why have you posted a question on Web Forms in the Win Forms section?
Deja View - the feeling that you've seen this post before.
Hello, In fact i've been misunderstood. I am talking about Winforms not Webforms. You know that in Webforms you have predfined classes that allows the persistence of variables (or objects) and sharing them between Webforms (Application and Session classes). What I want is to implement something similar in Winforms, i.e declaring some variable in Form1, and using it in Form2 .. FormN.. I guess that the first answer of gajatko would help. Thank you.
-
I would use a static class with a static member with an indexer:
public static class SC
{
private static SettingsClass settings = new SettingsClass();
public static SettingsClass Application
{
get { return settings; }
}
public class SettingsClass
{
Dictionary<string, object> settings = new Dictionary<string,object>();
public object this[string id]
{
get
{
return settings[id];
}
set
{
if (!settings.ContainsKey(id))
settings.Add(id, value);
else
settings[id] = value;
}
}
}
}add use it this way:
SC.Application["key"] = someObject;
Hope it helps anyhow.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Hello, Thanks gajatko for your help. Your code uses generics and SettingsClass that are not available in C# 1.1. How can I rewrite it to make it work in VS2003? Thanks. -- modified at 10:25 Monday 24th September, 2007
Implement System.Collections.DictionaryBase.
public static class SC
{
private static SettingsClass settings = new SettingsClass();
public static SettingsClass Application
{
get { return settings; }
}
public class SettingsClass
{
StringObjectDictionary settings = new StringObjectDictionary();
public object this[string id]
{
get
{
return settings[id];
}
set
{
if (!settings.ContainsKey(id))
settings.Add(id, value);
else
settings[id] = value;
}
}
}
}
class StringObjectDictionary : System.Collections.DictionaryBase
{
public StringObjectDictionary() : base()
{
}
public void Add(string key, object value)
{
base.Dictionary.Add(key, value);
}
public object this[string key]
{
get { return base.Dictionary[key]; }
set { base.Dictionary[key] = value; }
}
public bool ContainsKey(string key)
{
return base.Dictionary.Contains(key);
}
}Edit: The alternative is use of environment variables (
Environment.SetEnvironmentVariable
). They also are session-like. -- modified at 11:16 Monday 24th September, 2007Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Hey, I need to share variables among forms in the Web session (or application) manner. I used to have some code - which I don't have now- that implements the singleton design pattern and allows adding and retrieving objetcs this way :
Application["MyObject"] = object;
Can you help? Thanks.You can use public variables in this manner, public static class Global { public static string fromForm; } you can assign a value to fromForm variable in form1 and you can use it in form2 another way is declare public variable in form2 and pass a value to form2 as a parameter when you call form2 from form1 -- modified at 2:01 Thursday 27th September, 2007