如何使全局变量?全局变量

2023-09-07 01:10:22 作者:别致情书

在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

 
精彩推荐
图片推荐