如何使全局变量? [英] How to make global variables?

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

问题描述

在Windows应用程序开发中使用 C#.NET ,你如何让一个类,然后可以直接使用其他所有窗口形式的全局变量或全局实例, 例如Form1上,窗口2等。

In Windows application development using C# .NET, how do you make a global variable or global instance of a class, which can then be directly used by all other windows forms, e.g. form1, form2, etc.

推荐答案

创建,使instace可创建一次,在整个应用程序中使用单例类

Create singleton class so that instace can be created once and used across application

public class Global 
{
    private static readonly Global instance = new Global();
    public static Global Instance
    {
        get
        {
            return instance;
        }
    }

    Global()
    {
    }
    public string myproperty
    {
        get;set;
    }
    }

使用方法:     Global.Instance.myproperty

Usage: Global.Instance.myproperty

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

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