Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Session like variables

Session like variables

Scheduled Pinned Locked Moved Windows Forms
designregexarchitecturehelpquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    benqazou
    wrote on last edited by
    #1

    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.

    L A P B 5 Replies Last reply
    0
    • B benqazou

      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.

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • B benqazou

        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.

        L Offline
        L Offline
        Lutoslaw
        wrote on last edited by
        #3

        You can also use cookies, whose default expiration time is exactly one session.

        Request.Cookies.Add(...); // add cookie
        Request.Cookies[id]... // access cookie

        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.

        1 Reply Last reply
        0
        • B benqazou

          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.

          A Offline
          A Offline
          Arjan Einbu
          wrote on last edited by
          #4

          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; }
          }
          

          }

          1 Reply Last reply
          0
          • B benqazou

            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.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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.

            B 1 Reply Last reply
            0
            • P Pete OHanlon

              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.

              B Offline
              B Offline
              benqazou
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • L Lutoslaw

                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.

                B Offline
                B Offline
                benqazou
                wrote on last edited by
                #7

                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

                L 1 Reply Last reply
                0
                • B benqazou

                  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

                  L Offline
                  L Offline
                  Lutoslaw
                  wrote on last edited by
                  #8

                  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, 2007

                  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.

                  1 Reply Last reply
                  0
                  • B benqazou

                    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.

                    B Offline
                    B Offline
                    bttds
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups