在Windows窗体应用程序中使用全局/会话变量 [英] Using Global / Session variables in a Windows Forms application

查看:56
本文介绍了在Windows窗体应用程序中使用全局/会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的背景是基于网络的。我正在尝试编写一个Windows

应用程序,我发现一些简单的事情很困难。


目前我正在尝试找出如何存储信息会话

宽。在网络中你有会话状态,你可以添加变量或

对象。它们一直存在,直到会话结束。


我怎么能为Windows窗体应用程序执行此操作?


例如:模态窗体启动登录后,我做了一个数据库

查找,确认这是一个有效的用户。我想设置我的''User''

对象(我创建的一个类)并根据

为刚刚登录的人分配值然后我希望能够从我的其他表格中访问那个

对象。


我不知道怎么做。 (我可以为网络做这个,但这是Windows,

目标是Win2k或WinXP,如果重要的话)。

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

推荐答案



使用Singleton对象。

Public NotInheritable Class User

Public ReadOnly Shared Instance As New User

Private Sub New()

End Sub

结束班级


然后你可以从任何地方引用用户。实例并获取用户

对象。或者您可以使用User类的共享成员。


HTH,


Sam

2005年3月7日10:28:14 -0800, sa***@murdocks.on.ca (Sandy)写道:

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:
我的背景是基于网络的。我正在尝试编写一个Windows
应用程序,我发现一些简单的事情很困难。

目前我正在努力寻找如何存储信息会议的广泛性。在Web中,您具有会话状态,您可以向其添加变量或
对象。它们一直存在,直到会话结束。

我怎么能为Windows窗体应用程序执行此操作?

例如:启动模式窗口登录,我做数据库
查找,确认这是一个有效的用户。我想设置我的''User''
对象(我创建的一个类)并根据刚登录的人分配值。然后我想能够从我的其他形式访问
对象。

我不知道如何。 (我可以在网上做这个,但这是Windows,如果重要的话,目标是Win2k或WinXP。)
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正在招聘一个华盛顿特区的VB.NET

开发者为WinForms + WebServices职位。

中级到高级开发人员。对于

信息或将电子邮件简历应用于

sam_blinex_com。



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.


" Sandy" < SA *** @ murdocks.on.ca> schrieb:
"Sandy" <sa***@murdocks.on.ca> schrieb:
目前我正试图找出如何存储信息会议的广泛性。在Web中,您具有会话状态,您可以向其添加变量或
对象。它们一直存在直到会话结束。

我怎么能为Windows窗体应用程序执行此操作?
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?




只需添加一个设置类,实现Singleton设计模式,例如
。或者,您可以使用具有属性的模块,其中包含

设置。关于Singleton设计模式的信息可以在这里找到

(一些文章是用C#编写的,但文本也适用于VB.NET

):


在C#中实现单例模式

< URL:http://www.yoda.arachsys.com/csharp/singleton.html>


探索单身人士设计模式

< URL:http://msdn.microsoft.com/library/en-us/dnbda/html/singletondespatt.asp>


设计模式:C中的单身人士#

< URL:http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id = 486>


设计模式:Singleton

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

-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://dotnet.mvps.org/dotnet/faqs/>



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


谢谢。这当然听起来就像我想做的那样。


这就是我(不成功)尝试实现它的方式。


首先,我为所有用户打了一个类:用户


这是我将用于将用户添加到数据库的类,

修改等等等


因为这个课程的这么多和我想要的一样完全相同

(现在称为...)CurrentUser

我以为我会继续将User转换为CurrentUser,然后(我的理论是这样的)我只需要添加代码来使它成为单例,否则它只是

会很好。


这是我做的方式(注意它不起作用)。


< code>


Public NotInheritable Class CurrentUser

继承用户


<! - 自动生成的代码去了这里 - >


共享myInstance作为CurrentUser


公共共享函数GetInstance()As CurrentUser
如果myInstance什么都没有那么

myInstance =新的CurrentUser

结束如果

返回myInstance

结束功能


结束课程

< / code>


我以为我会打电话给像这样的当前用户的实例:

将objCurrentUser调暗为CurrentUser.GetInstance()


但是,GetInstance函数不可访问,因此没有

工作。


有什么东西我显然做错了吗? (我会在interum中继续阅读

,但是有很多文章,所有这些都这样做

略有不同......嘿......为什么呢很多都必须是c#;)


Sandy Murdock MCP

***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励为它!
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!


这篇关于在Windows窗体应用程序中使用全局/会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆