创建一个全局对象? [英] Creating a Global Object?

查看:123
本文介绍了创建一个全局对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现以下目标:

I'm trying to achieve the following:

成功验证用户身份后,我需要创建一个Global用户对象,可以从不同的Form(或类)访问该对象.

When a user has been successfully authenticated, I need to create a Global user object which I can access from different Forms (or classes).

我在浏览VS中可用的数据源时,发现有一个对象"选项可能适合于我要实现的目标.麻烦的是,我不知道它是如何工作的.

I was looking through the Data Sources available in VS, and saw there's a "Object" option which may be suitable for what I'm trying to achieve. The trouble is, I have no idea how it works.

有人能指出我正确的方向吗?

Can anyone point me in the right direction?

谢谢.

推荐答案

假定这是Windows Forms应用程序,则可以创建一个存储在静态ApplicationState类中的User类.

Assuming that this is a Windows Forms application, you can create a User class that is stored in a static ApplicationState class.

步骤:

1)创建用户类以保存有关用户的信息:

1) Create your user class to hold information about the user:

public class User
{
    public string Login { get; set; }
    //.. other properties
}

2)创建您的ApplicationState类:

2) Create your ApplicationState class:

public static class ApplicationState
{
    public static User CurrentUser { get; set; }
}

3)在登录过程中,创建新版本的用户类并将其分配给ApplicationState.CurrentUser属性:

3) In your login process, create a new version of the user class and assign it to the ApplicationState.CurrentUser property:

    public void CompleteLogin(string sLogin)
    {
        User user = new User();

        user.Login = sLogin;

        ApplicationState.CurrentUser = user;
    }

4)您现在可以在项目的几乎任何地方使用ApplicationState.CurrentUser.

4) You can now use ApplicationState.CurrentUser just about anywhere in your project.

这篇关于创建一个全局对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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