By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Using Global / Session variables in a Windows Forms application

P: n/a
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).
Nov 21 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a

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, sa***@murdocks.on.ca (Sandy) wrote:
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).


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.
Nov 21 '05 #2

P: n/a
"Sandy" <sa***@murdocks.on.ca> schrieb:
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?


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.arachsys.com/csharp/singleton.html>

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

Design Pattern: Singleton in C#
<URL:http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=486>

Design Patterns: Singleton
<URL:http://www.dofactory.com/Patterns/PatternSingleton.aspx>

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

Nov 21 '05 #3

P: n/a
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.GetInstance()

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!
Nov 21 '05 #4

P: n/a
"Sandy Murdock" <sa***@murdocks.on.ca> schrieb:
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.GetInstance()

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


Simply use 'CurrentUser.GetInstance().Name = "Bla"'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
I am not clear on where or how to use:

Simply use 'CurrentUser.GetInstance().Name = "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!
Nov 21 '05 #6

P: n/a


--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
"Sandy Murdock" <sa***@murdocks.on.ca> schrieb im Newsbeitrag
news:OR**************@TK2MSFTNGP12.phx.gbl...
I am not clear on where or how to use:

Simply use 'CurrentUser.GetInstance().Name = "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!


Nov 21 '05 #7

P: n/a
"Sandy Murdock" <sa***@murdocks.on.ca> schrieb im Newsbeitrag
news:OR**************@TK2MSFTNGP12.phx.gbl...
I am not clear on where or how to use:

Simply use 'CurrentUser.GetInstance().Name = "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!

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

Nov 21 '05 #8

P: n/a
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" <sa***@murdocks.on.ca> schrieb im Newsbeitrag
news:OR**************@TK2MSFTNGP12.phx.gbl...
I am not clear on where or how to use:

Simply use 'CurrentUser.GetInstance().Name = "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!


Nov 21 '05 #9

P: n/a
"Sandy Murdock" <sa***@murdocks.on.ca> schrieb:
I am not clear on where or how to use:

Simply use 'CurrentUser.GetInstance().Name = "Bla"'.

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

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/>

Nov 21 '05 #10

P: n/a
'CurrentUser.GetInstance

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(ByVal 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!
Nov 21 '05 #11

P: n/a
"Sandy Murdock" <sa***@murdocks.on.ca> schrieb:
'CurrentUser.GetInstance

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(ByVal strValue As String) As


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/>
Nov 21 '05 #12

P: n/a
I have changed:

Public Shared Function GetInstance(ByVal 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!
Nov 21 '05 #13

P: n/a
"Sandy Murdock" <sa***@murdocks.on.ca> schrieb:
Public Shared Function GetInstance() As CurrentUser

The function is still not accessible.


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.GetInstance()'.

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

Nov 21 '05 #14

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


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.
Nov 21 '05 #15

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:<#M**************@TK2MSFTNGP09.phx.gbl>...
"Sandy Murdock" <sa***@murdocks.on.ca> schrieb:
I am not clear on where or how to use:

Simply use 'CurrentUser.GetInstance().Name = "Bla"'.

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

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.


I have been away from this program for some time (surgery, nothing
life threatening but I have been off work.) so I have now done the
following:

Created the class as outlined:

Public NotInheritable Class Settings

Private Shared m_DefInstance As Settings

Dim 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

Then in my program I do this:

Sub SetFormToUser(ByVal objUser As User)

Dim objThisUser As Settings
objThisUser.GetInstance()

MessageBox.Show(objUser.FirstName)
objThisUser.UserName = objUser.FirstName

Me.UserID = objUser.UserID
Me.UserPwd = objUser.Pwd
Me.FirstName = objUser.FirstName
Me.LastName = objUser.LastName
Me.UserTypeID = objUser.UserTypeID
Me.UserType = objUser.UserType
End Sub

This code is on the actual login form. I set the properties for the
form (which ultimately I hope to do away with) and, in theory, set the
the UserName property from the Settings Class (called objThis user).

Here is the problem;

I can confirm that objUser.FirstName has a value, but when I try to
run the code I get an error: "Object reference not set to an instance
of an object."

I believe this is caused by the objThisUser (Settings) object,
UserName property not existing? There is some strange thing happening
here; I know that I am assigning a value, but I am getting the 'Null'
message.

Do you have any idea what might cause that?
Nov 21 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.