声明一个pubilc变量 [英] Declare a pubilc variable

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

问题描述

大家好。

我在Windows窗体中声明一个公共变量,并用这种形式的字符串初始化

现在我想以其他形式使用这个变量。但是新形式的变量是null。

我在做什么?

plz help

Hello guys.
I declare a public variable in a windows Form and it initializes with a string in this form
Now i wanna use this variable in other form.but variable in new form is null.
What am i doing ?
plz help

推荐答案

添加新的类的模块并在此模块中将此变量声明为public。



有关在C#中作用域的更多信息: http://msdn.microsoft.com/en-us/library/aa691132%28v=vs.71%29.aspx [ ^ ]



示例: http: //stackoverflow.com/questions/2445436/global-variables-in-c-net [ ^ ]
Add new module of class and declare this variable as public in this module.

More about scoping in C#: http://msdn.microsoft.com/en-us/library/aa691132%28v=vs.71%29.aspx[^]

Example: http://stackoverflow.com/questions/2445436/global-variables-in-c-net[^]


使用单独的类文件...

Use a separate class file...
public static class GlobalVar
    {
        /// <summary>
        /// Global variable that is constant.
        /// </summary>
        public const string GlobalString = "Important Text";

        /// <summary>
        /// Static value protected by access routine.
        /// </summary>
        static int _globalValue;

        /// <summary>
        /// Access routine for global variable.
        /// </summary>
        public static int GlobalValue
        {
            get
            {
                return _globalValue;
            }
            set
            {
                _globalValue = value;
            }
        }

        /// <summary>
        /// Global static field.
        /// </summary>
        public static bool GlobalBoolean;
    }


如果要从任何地方访问变量,则应将其声明为静态。在这种情况下,一旦初始化 - 可以从项目中的任何地方读取该值。
If you want a variable to be accessed from anywhere then you should declare that as static. In that case once initialized - that value can be read from anywhere in the project.


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

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