473,473 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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


--
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
"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
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
"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
'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
"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
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
"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
> 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: RooLoo | last post by:
Hey all In my GLOBAL.ASA file I'm trying to create a session variable for reference in the various webpages of my site.... Sub Session_OnStart Session("LoggedOn")="Y" End Sub When...
5
by: Prabhat | last post by:
Hi All, My website is hosted with the ISP and recently I have modified my Global.asa file for the ASP application. So I think we need to restart the World Wide Web Publishing Service so that...
2
by: David McCormack | last post by:
I have a small project that I'm writing to help me learn C# and the .Net Framework. This project is a WinForms program with multiple forms that accesses MSDE. I've got most of it done but I've hit...
12
by: John M | last post by:
Hello, On Microsoft Visual Studio .NET 2003, I want to use some global elements, that can be used in each one of my pages. i.e I put a oleDBConnection on global.asax.vb How can I use it...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
15
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with 10 items it might use 1KB, and if I load it with...
1
by: None | last post by:
Hi, I want to maintain gloabl variables in windows forms like session in asp.net. How can i do this. Is there anyway to do this? If anybody knows the solution pls let me know.. Thanks and...
11
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
0
by: tharika_c | last post by:
Hi, We have a simple ASP.NET web application where one of the Session variables, called Session("SSO_ID") gets created and assigned a value (equal to the HTTP_HRID request variable value),...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.