是否可以在Windows窗体中定义全局变量? [英] is it possible to define a global variable in windows form?

查看:266
本文介绍了是否可以在Windows窗体中定义全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中有一个基础表格。在此表单中,有一个文档选项卡,我的用户控件将以基本形式加载到单独的文档窗口中。我希望有一个全局变量,所有用户控件都可以看到它。我怎么能这样做

In my project there is a Base form. In this form there is a document tab which my user controls will loaded in separate document windows in Base form. I want to have a global variable that all user controls see that. How can I do this

推荐答案

形式上,.NET没有相同意义上的全局字段和其他语言(请参阅我对解决方案1的评论)。这很好:你从来没有真正需要它们;使用全局变量会影响代码质量。但是,解决方案1建议功能上与您想要的相同。



但是你甚至不需要它。你需要一些单身人士。如果您提到的表单是主表单,那么在几乎所有情况下,它都是单例。因此,您应该更好地拥有此表单的常规实例(非静态)成员。您始终可以从代码的其他位置通过此表单实例访问它。不过,你可以让它内部静态,但这不是一个优雅的解决方案。



如果你真的需要单身,你需要使用单身模式正确: http://en.wikipedia.org/wiki/Singleton_pattern [ ^ ]。



现在,单身人士有许多不正确或不好,不受支持或不安全的实施。在这里,您将找到正确的实施方案: http://csharpindepth.com/Articles/General/Singleton.aspx [ ^ ]。



事件正确实现的模式是有问题的。它只是意味着它应该很少使用并且小心使用。请阅读:

http://code.google.com/p / google-singleton-detector / wiki / WhySingletonsAreControversial [ ^ ],

http:/ /www-106.ibm.com/developerworks/library/co-single.html [ ^ ]。



-SA
Formally, .NET does not have global fields in the same sense and in other languages (please see my comment to Solution 1). And this is good: you never really need them; using global variables compromises the quality of code. However, Solution 1 suggests a functionally equivalent to what you want.

But you don't really need even that. You need some singleton. If the form you mentioned is a main form, it is, in almost all cases, is already a singleton. So, you should better have a regular instance (non-static) members of this form. You can always access it through this form instance from other places of the code. Still, you can make it internal static, but this is not an elegant solution.

If you really need a singleton, you need to use the singleton pattern correctly: http://en.wikipedia.org/wiki/Singleton_pattern[^].

Now, there are a number of incorrect or just bad, unsupportable or unsafe implementation of the singleton. Here you will find good correct implementations: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

Event the correctly implemented pattern is questionable. It simply means that it should be used quite rarely and with care. Please read:
http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial[^],
http://www-106.ibm.com/developerworks/library/co-single.html[^].

—SA


.NET没有全局变量,但你可以通过创建一个带有静态变量的类来做类似的事情。这个类可以是静态的,但这不是必需的。但其中的变量必须是静态的:

.NET doesn't have global variables, but you can do something similar by creating a class with static variables. This class can be static, but that's not required. But the variables in it have to be static:
public static class GlobalVariables
{
    // Add your global variables here (don't forget to make the variables static). For example:
    public static string GlobalString; // or 'internal' if you don't want that your variables can be accessed from another assembly
    public static int GlobalInt = 5;
}



在另一个班级中,使用这种方式访问​​它们:


And in another class, access them using this way:

GlobalVariables.GlobalString = "put your text here";
int i = GlobalVariables.GlobalInt;


这篇关于是否可以在Windows窗体中定义全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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