在 vb 中声明全局变量 [英] declaring global variables in vb

查看:62
本文介绍了在 vb 中声明全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户点击一个按钮,生成一个新的表单,用户输入他/她的密码?如何在内存中保留用户名副本?

The user clicks a button, a new form is generated where the user enters his/her password? How can I keep an in-memory copy of the username?

例如在网络表单中,我可以只使用会话(用户").我怎样才能用winforms复制这个?

For instance in web forms, I can just use Session ("User"). How can I replicate this with winforms?

谢谢

推荐答案

您可以使用静态(在 VB .NET 中共享)对象

You could use a static (Shared in VB .NET) object

示例

Public Class SessionInformation

   Public Shared Property UserPassword As String

End Class

然后您可以通过调用访问此

You can then access this by calling

SessionInformation.UserPassword

由于似乎有人担心这是否是一个有效的实现,我将解释为什么这不是最好的主意.

Since there seems to be some concerns revolving around whether or not this is a valid implementation I will explain why this is not the best idea.

这将创建 SessionInformation 的单个实例,这意味着如果它在程序中的任何地方发生更改,它都会影响程序的其余部分.

This creates a single instance of SessionInformation which means that if it is changed anywhere in the program it affects the rest of the program.

如果您不使用数据库(SQL、Access、文件等)来维护要在以后检索的对象的内存不足副本,则维护对象持久性可能会很困难.实现这一点的最简单方法是使用位于您的应用程序文件夹中的 SQLCE 数据库,并使用标准 T-SQL 来存储您需要的信息.但是,在密码的情况下,由于安全问题,这可能并不理想.

Maintaining object persistence can be difficult if you aren't using a database (SQL, Access, a file, etc...) to maintain an out of memory copy of the object that you want to retrieve at a later date. The easiest way to implement this would be to use a SQLCE database that live in your application folder and using standard T-SQL to store the information that you need. However in the case of a password this may be non-ideal due to security issues.

或者登录和注销用户的更好方法是使用 System.Security.Principal 命名空间为您的应用程序创建用户.

Or the better way for logging in and out user would be to make use of the System.Security.Principal Namespace to create a user for your application.

这篇关于在 vb 中声明全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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