473,785 Members | 2,484 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
15 22571
'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!
Nov 21 '05 #11
"Sandy Murdock" <sa***@murdocks .on.ca> schrieb:
'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


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(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!
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.GetIn stance()'.

--
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******* *******@TK2MSFT NGP09.phx.gbl>. ..
"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.


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(B yVal objUser As User)

Dim objThisUser As Settings
objThisUser.Get Instance()

MessageBox.Show (objUser.FirstN ame)
objThisUser.Use rName = objUser.FirstNa me

Me.UserID = objUser.UserID
Me.UserPwd = objUser.Pwd
Me.FirstName = objUser.FirstNa me
Me.LastName = objUser.LastNam e
Me.UserTypeID = objUser.UserTyp eID
Me.UserType = objUser.UserTyp e
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.FirstNa me 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
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
3829
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
14783
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
2351
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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
10147
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...
0
9950
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...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.