473,769 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ 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 22567

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

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.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!
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.Get Instance()

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


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/>
Nov 21 '05 #5
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!
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******** ******@TK2MSFTN GP12.phx.gbl...
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!


Nov 21 '05 #7
"Sandy Murdock" <sa***@murdocks .on.ca> schrieb im Newsbeitrag
news:OR******** ******@TK2MSFTN GP12.phx.gbl...
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!

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******** ******@TK2MSFTN GP12.phx.gbl...
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!


Nov 21 '05 #9
"Sandy Murdock" <sa***@murdocks .on.ca> schrieb:
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?

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
8867
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 referring to Session("LoggedOn") on my various ASP pages, it is coming
5
6248
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 the changes in the Global.asa will take effect. I think it will be difficult for the ISP to restart the Service as there many applications will be running. So can you suggest how can my global.asa file changes will take effect so that I can deply...
2
27785
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 a slight brick wall. Does anyone have any recommended strategies for dealing with application wide variables? i.e. the user name of the current user that's logged in to the application rather than the PC. The best idea I've come up with so...
12
3828
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 (the oleDBConnection on global.asa.vb) at the other aspx pages ?
15
4779
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 button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
15
2419
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 1000 items it might use 100KB. How do I measure the amount of memory used once the class is loaded? Thanks! -- Joe Fallon
1
14782
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 Regards, Vinothkumar B bvinoth@tvsinfotech.com
11
2350
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
0
2291
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), inside the Session_Start event of the global.asax.vb file, since this variable is accessed by almost all the pages in the project. Besides this variable, there are other session variables which are set in the various aspx pages, as needed.
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5310
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
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 we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.