C# - Winforms - 全局变量 [英] C# - Winforms - Global Variables

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

问题描述

我希望一些变量在整个项目中是全局的,并且可以以各种形式访问.我该怎么做?

I want some variables to be global across the project and accessible in every form. How can I do this?

推荐答案

是的,您可以使用静态类.像这样:

yes you can by using static class. like this:

static class Global
{
    private static string _globalVar = "";

    public static string GlobalVar
    {
        get { return _globalVar; }
        set { _globalVar = value; }
    }
}

并使用您可以编写的任何地方:

and for using any where you can write:

GlobalClass.GlobalVar = "any string value"

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

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