如何声明全局变量 [英] How to declare Global variable

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

问题描述

我正在Visual Studio 2010中使用c#和WPF的项目中工作....
我想使用布尔类型的全局变量,该变量在主窗口中定义,我还有其他页面正在使用该控件...
请帮帮我...
在此先感谢

Am working in project which use c# and WPF in visual studio 2010....
I want to use global variable which is of Boolean type and which is defined in main window and i have other page in which am using that control...
Please help me...
Thanks in advance

推荐答案

您不应在问题中使用大写字母.它被视为在公共论坛上大喊大叫,非常不礼貌.您不应该在c#中使用Global变量,因为这被认为是不好的做法,您应该创建一个公共属性,该属性在您的主表单中返回一个布尔值,并将其作为参数传递给您的form2构造函数,以便form2可以使用和更改适当的属性.
You shouldn''t use capitals in your question. It is deemed as shouting on public forums and is extremely rude. You should not use Global variables in c# either as that is considered bad practice, you should create a public property that returns a boolean in your main form and pass that as a parameter in your constructor for form2, so that form2 can use and change the property appropriately.


.NET中没有全局变量的概念(看……最后;谢天谢地!).

最接近的模拟物是某个类的静态字段,都具有内部或公共访问修饰符.不建议这样做.

是的,正如Wayne建议的那样,您可以引用在需要的入口点处声明的堆栈变量的引用(请参见我对他的回答的评论).另一个解决方案是单一设计模式,请参见:
http://en.wikipedia.org/wiki/Singleton_pattern [ http://en.wikipedia.org/wiki/Design_pattern_(computer_science) [ http://csharpindepth.com/Articles/General/Singleton.aspx [
There is no a concept of global variable in .NET (sight… finally; thanks goodness!).

The closest analog is a static field of some class, both having access modifier internal or public. This is not recommended.

Yes, you can have a reference to a stack variable declared in entry point passed where it is needed, as Wayne suggested (please also see my clarification in my comment to his answer); another solution is the singleton design pattern, see:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)[^],
http://csharpindepth.com/Articles/General/Singleton.aspx[^] (good implementation samples here).

—SA


添加新类并将其命名为全局变量
delcare该类上的几个变量
现在在您的项目中调用该类
为变量赋值,现在您可以在任何地方访问此变量了
在您的项目中
add new class & name it global variable
delcare few variables on that class
now in your project call that class
assign value to variable , now u can access this variable anywhere
in your project


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

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