Using Global / Session variables in a Windows Forms application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandy

    Using Global / Session variables in a Windows Forms application

    My background is web based. I am attempting to write a Windows
    application and I am finding some simple things difficult.

    Currently I am trying to find out how to store information session
    wide. In the web you have session state and you can add variables or
    objects to it. They remain in existance until the session ends.

    How could I do that for a windows forms application?

    For example: a modal form is launched to log in, I do a database
    lookup, confirm this is a valid user. I would like to set up my 'User'
    object (a class that I have created) and assign values to it based on
    the person who just logged in. I then want to be able to access that
    object from my other forms.

    I don't know how. (I could do this for the web, but this is Windows,
    target is Win2k or WinXP if it matters).
  • Samuel R. Neff

    #2
    Re: Using Global / Session variables in a Windows Forms application


    Use a Singleton object.

    Public NotInheritable Class User
    Public ReadOnly Shared Instance As New User
    Private Sub New()
    End Sub
    End Class

    Then from anywhere you can reference User.Instance and get the user
    object. Or you can use shared members of the User class.

    HTH,

    Sam


    On 7 Mar 2005 10:28:14 -0800, sandy@murdocks. on.ca (Sandy) wrote:
    [color=blue]
    >My background is web based. I am attempting to write a Windows
    >application and I am finding some simple things difficult.
    >
    >Currently I am trying to find out how to store information session
    >wide. In the web you have session state and you can add variables or
    >objects to it. They remain in existance until the session ends.
    >
    >How could I do that for a windows forms application?
    >
    >For example: a modal form is launched to log in, I do a database
    >lookup, confirm this is a valid user. I would like to set up my 'User'
    >object (a class that I have created) and assign values to it based on
    >the person who just logged in. I then want to be able to access that
    >object from my other forms.
    >
    >I don't know how. (I could do this for the web, but this is Windows,
    >target is Win2k or WinXP if it matters).[/color]

    B-Line is now hiring one Washington D.C. area VB.NET
    developer for WinForms + WebServices position.
    Seaking mid to senior level developer. For
    information or to apply e-mail resume to
    sam_blinex_com.

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Using Global / Session variables in a Windows Forms application

      "Sandy" <sandy@murdocks .on.ca> schrieb:[color=blue]
      > Currently I am trying to find out how to store information session
      > wide. In the web you have session state and you can add variables or
      > objects to it. They remain in existance until the session ends.
      >
      > How could I do that for a windows forms application?[/color]

      Simply add a settings class that implements the Singleton design pattern,
      for example. Alternatively you can use a module with properties which hold
      the settings. Information on the Singleton design pattern can be found here
      (some of the articles are written in C#, but the text does apply to VB.NET
      too):

      Implementing the Singleton Pattern in C#
      <URL:http://www.yoda.arachs ys.com/csharp/singleton.html>

      Exploring the Singleton Design Pattern
      <URL:http://msdn.microsoft. com/library/en-us/dnbda/html/singletondespat t.asp>

      Design Pattern: Singleton in C#
      <URL:http://www.devhood.com/tutorials/tutorial_detail s.aspx?tutorial _id=486>

      Design Patterns: Singleton
      <URL:http://www.dofactory.c om/Patterns/PatternSingleto n.aspx>

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Sandy Murdock

        #4
        Re: Using Global / Session variables in a Windows Forms application

        Thanks. This certainly sounds like what I am trying to do.

        Here is how I am (un-successfully) trying to implement it.

        First, I have a class for all users called: User

        This is the class that I will be using to add users to the database,
        modify etc. etc.

        Since so much of this class is the exact same as what I want for the
        (now called...) CurrentUser

        I thought I would inherit User into CurrentUser, then (my theory goes) I
        would only need to add the code to make it a singleton, otherwise it
        would be good to go.

        Here is how I did it (Note it does not work).

        <code>

        Public NotInheritable Class CurrentUser
        Inherits User

        <!--Auto generated code went here-->

        Shared myInstance As CurrentUser

        Public Shared Function GetInstance() As CurrentUser
        If myInstance Is Nothing Then
        myInstance = New CurrentUser
        End If
        Return myInstance
        End Function

        End Class
        </code>

        I thought that I would call an instance of the Current user like this:

        Dim objCurrentUser as CurrentUser.Get Instance()

        However, the GetInstance function is not accessible so that has not
        worked.

        Is there something that I am obviously doing wrong? (I will keep reading
        in the interum, but there are a lot of articles, all of which do this
        slightly differently... and hey... why do so many have to be c# ;)

        Sandy Murdock MCP


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Using Global / Session variables in a Windows Forms application

          "Sandy Murdock" <sandy@murdocks .on.ca> schrieb:[color=blue]
          > First, I have a class for all users called: User
          >
          > This is the class that I will be using to add users to the database,
          > modify etc. etc.
          >
          > Since so much of this class is the exact same as what I want for the
          > (now called...) CurrentUser
          >
          > I thought I would inherit User into CurrentUser, then (my theory goes) I
          > would only need to add the code to make it a singleton, otherwise it
          > would be good to go.
          >
          > Here is how I did it (Note it does not work).
          >
          > <code>
          >
          > Public NotInheritable Class CurrentUser
          > Inherits User
          >
          > <!--Auto generated code went here-->
          >
          > Shared myInstance As CurrentUser
          >
          > Public Shared Function GetInstance() As CurrentUser
          > If myInstance Is Nothing Then
          > myInstance = New CurrentUser
          > End If
          > Return myInstance
          > End Function
          >
          > End Class
          > </code>
          >
          > I thought that I would call an instance of the Current user like this:
          >
          > Dim objCurrentUser as CurrentUser.Get Instance()
          >
          > However, the GetInstance function is not accessible so that has not
          > worked.[/color]

          Simply use 'CurrentUser.Ge tInstance().Nam e = "Bla"'.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Sandy Murdock

            #6
            Re: Using Global / Session variables in a Windows Forms application

            I am not clear on where or how to use:

            Simply use 'CurrentUser.Ge tInstance().Nam e = "Bla"'.

            I have tried a few places and modifications but none seems to be a valid
            command. Is that a C# style command?

            Sandy Murdock MCP

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Using Global / Session variables in a Windows Forms application



              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
              "Sandy Murdock" <sandy@murdocks .on.ca> schrieb im Newsbeitrag
              news:ORBEW97IFH A.2356@TK2MSFTN GP12.phx.gbl...[color=blue]
              >I am not clear on where or how to use:
              >
              > Simply use 'CurrentUser.Ge tInstance().Nam e = "Bla"'.
              >
              > I have tried a few places and modifications but none seems to be a valid
              > command. Is that a C# style command?
              >
              > Sandy Murdock MCP
              >
              > *** Sent via Developersdex http://www.developersdex.com ***
              > Don't just participate in USENET...get rewarded for it![/color]

              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Using Global / Session variables in a Windows Forms application

                "Sandy Murdock" <sandy@murdocks .on.ca> schrieb im Newsbeitrag
                news:ORBEW97IFH A.2356@TK2MSFTN GP12.phx.gbl...[color=blue]
                >I am not clear on where or how to use:
                >
                > Simply use 'CurrentUser.Ge tInstance().Nam e = "Bla"'.
                >
                > I have tried a few places and modifications but none seems to be a valid
                > command. Is that a C# style command?
                >
                > Sandy Murdock MCP
                >
                > *** Sent via Developersdex http://www.developersdex.com ***
                > Don't just participate in USENET...get rewarded for it![/color]
                a
                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Using Global / Session variables in a Windows Forms application

                  ffff

                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
                  "Sandy Murdock" <sandy@murdocks .on.ca> schrieb im Newsbeitrag
                  news:ORBEW97IFH A.2356@TK2MSFTN GP12.phx.gbl...[color=blue]
                  >I am not clear on where or how to use:
                  >
                  > Simply use 'CurrentUser.Ge tInstance().Nam e = "Bla"'.
                  >
                  > I have tried a few places and modifications but none seems to be a valid
                  > command. Is that a C# style command?
                  >
                  > Sandy Murdock MCP
                  >
                  > *** Sent via Developersdex http://www.developersdex.com ***
                  > Don't just participate in USENET...get rewarded for it![/color]

                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: Using Global / Session variables in a Windows Forms application

                    "Sandy Murdock" <sandy@murdocks .on.ca> schrieb:[color=blue]
                    >I am not clear on where or how to use:
                    >
                    > Simply use 'CurrentUser.Ge tInstance().Nam e = "Bla"'.
                    >
                    > I have tried a few places and modifications but none seems to be a valid
                    > command. Is that a C# style command?[/color]


                    Sorry for the other messages -- my keyboard temporarily didn't work for some
                    reasons.

                    You can use the code above everywhere in your code after you added the
                    Singleton class to your project. '.Name = "Bla"' is only a sample that
                    presumes that you have added a 'Name' property to the 'CurrentUser' class.

                    --
                    M S Herfried K. Wagner
                    M V P <URL:http://dotnet.mvps.org/>
                    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                    Comment

                    • Sandy Murdock

                      #11
                      Re: Using Global / Session variables in a Windows Forms application

                      'CurrentUser.Ge tInstance

                      is just not available. I have used the code as documented above:

                      <code>
                      Public NotInheritable Class CurrentUser
                      Inherits User

                      Shared myInstance As CurrentUser

                      Public Shared Function GetInstance(ByV al strValue As String) As
                      CurrentUser
                      If myInstance Is Nothing Then
                      myInstance = New CurrentUser)
                      End If
                      Return myInstance
                      End Function

                      End Class
                      </code>

                      but GetInstance is not accessable from anywhere.

                      This looks pretty simple, I don't see where I have gone wrong, but it
                      certainly does not work.

                      Sandy Murdock MCP

                      *** Sent via Developersdex http://www.developersdex.com ***
                      Don't just participate in USENET...get rewarded for it!

                      Comment

                      • Herfried K. Wagner [MVP]

                        #12
                        Re: Using Global / Session variables in a Windows Forms application

                        "Sandy Murdock" <sandy@murdocks .on.ca> schrieb:[color=blue]
                        > 'CurrentUser.Ge tInstance
                        >
                        > is just not available. I have used the code as documented above:
                        >
                        > <code>
                        > Public NotInheritable Class CurrentUser
                        > Inherits User
                        >
                        > Shared myInstance As CurrentUser
                        >
                        > Public Shared Function GetInstance(ByV al strValue As String) As[/color]

                        Remove the 'strValue' parameter, then it should work...

                        --
                        M S Herfried K. Wagner
                        M V P <URL:http://dotnet.mvps.org/>
                        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                        Comment

                        • Sandy Murdock

                          #13
                          Re: Using Global / Session variables in a Windows Forms application

                          I have changed:

                          Public Shared Function GetInstance(ByV al strValue As String) As
                          CurrentUser

                          to

                          Public Shared Function GetInstance() As CurrentUser

                          The function is still not accessible.

                          Sandy Murdock MCP

                          *** Sent via Developersdex http://www.developersdex.com ***
                          Don't just participate in USENET...get rewarded for it!

                          Comment

                          • Herfried K. Wagner [MVP]

                            #14
                            Re: Using Global / Session variables in a Windows Forms application

                            "Sandy Murdock" <sandy@murdocks .on.ca> schrieb:[color=blue]
                            > Public Shared Function GetInstance() As CurrentUser
                            >
                            > The function is still not accessible.[/color]

                            Add the code below to your project:

                            \\\
                            Public NotInheritable Class Settings
                            Private Shared m_DefInstance As Settings

                            Private m_UserName As String

                            Private Sub New()
                            '
                            End Sub

                            Public Shared Function GetInstance() As Settings
                            If m_DefInstance Is Nothing Then
                            m_DefInstance = New Settings
                            End If
                            Return m_DefInstance
                            End Function

                            Public Property UserName() As String
                            Get
                            Return m_UserName
                            End Get
                            Set(ByVal Value As String)
                            m_UserName = Value
                            End Set
                            End Property
                            End Class
                            ///

                            You can access the settings in the project that contains the class with
                            'Settings.GetIn stance()'.

                            --
                            M S Herfried K. Wagner
                            M V P <URL:http://dotnet.mvps.org/>
                            V B <URL:http://classicvb.org/petition/>

                            Comment

                            • Rob Nicholson

                              #15
                              Re: Using Global / Session variables in a Windows Forms application

                              > My background is web based. I am attempting to write a Windows[color=blue]
                              > application and I am finding some simple things difficult.[/color]

                              It's usually the other way around :-) I come from a traditional client
                              application environment and I'm finding I'm having to re-think a lot of the
                              ways you do things like global variables stored in the session.

                              Come the revolution, we'll be back full circle to full-blown client apps
                              that happen to be internet hosted and so benefit from "access anywhere" and
                              "zero rollout/footprint". Mark my words.

                              Cheers, Rob.


                              Comment

                              Working...