如何在C#中使用全局变量 [英] How to use global variable in C#

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

问题描述

类中有一个全局变量(bool)。它会在if else中改变。我需要在另一个类中使用该变量的最终结果。我应该怎么做

There is a global variable(bool) in a class.It will change with in if else.I need to use final result of that variable in another class.what should I do

推荐答案

从长远来看,在编程中最大限度地减少全局变量,方法或函数等的使用是件好事:它可以实现更清晰的代码,更严格地控​​制可能的不可预测性代码中的交互。在多线程应用程序,多用户应用程序或客户端 - 服务器应用程序等中使用全局范围可能会产生其他复杂情况。



有些人几乎有宗教的信念是,任何对全局变量的使用本质上都是坏的,甚至是邪恶的。实际上,.NET本身的实现经常使用全局静态枚举,函数和方法。每次使用string.SubString(#,#)之类的方法时,都使用String类的静态方法。每次使用myTextBox.BackColor = Color.AliceBlue;你在System.Drawing中使用静态颜色枚举。



对于许多刚接触C#编程的人来说,如果习惯这个想法有点难以理解您可以在Form或UserControl中定义某些Type的静态方法,函数或变量,然后您已定义的方法,函数或变量 of Form或UserControl。无论Form或UserControl创建了多少个实例,都会有一个且只有一个变量,方法或函数的实例。



在.NET C#中使用全局变量的最常见方法是在您希望访问全局变量的其他类所使用的同一命名空间中定义静态类。



看看你是否觉得这个答案有用:如何在C#.net中定义全局可用:[ ^ ]。



我认为危险,对于初学程序员来说,在利用全局变量时,它可能成为一种避免学习如何传递数据或函数的更严格学科的方法,或方法,从一个对象到另一个对象。并且,仔细实现对象相互交谈的方式是未来世界编程的关键,大多数应用程序将成为多用户,多线程,客户端服务器等。



所以,当你说上面有另一个类应该知道第二类中布尔变量的改变值时:嗯,是的,你可以创建一个静态类,保持一个布尔值,它将在布尔值改变时随后由第二个类设置,然后另一个类总是可以通过使用静态变量来访问当前值。



但是,您也可以通过将消息从第二类传递到另一类来实现。



而且,您可以通过多种方式实现这一目标:通过事件;通过在另一个类可以访问的第二个类中公开一个公共属性,等等。



CodeProject的OriginalGriff(不接受模仿!)写了三篇关于如何编写的优秀教程在WinForms中实现消息传递:从表单到表单;从形式到孩子控制;并且,从孩子控制到表格。我强烈建议你研究那些:[ ^ ]。



这三篇文章中的技术很容易适应在没有UI的类之间传递数据(即表单,UserControls等。)
It's a good thing, in the long-run, to minimize the use of Global Variables, or Methods, or Functions, etc., in programming: it makes for cleaner code, and tighter control of possible unpredictable interactions in the code. Other complications may arise from use of global scope in multi-threading applications, or multi-user applications, or client-server applications, etc.

Some people have an almost "religious" conviction that any use of global variables is inherently bad, even "evil." In fact, the implementation of .NET itself uses global static enumerations, functions, and methods frequently. Every time you use a method like string.SubString(#,#) you are using a static method of the String Class. Every time you use something like myTextBox.BackColor = Color.AliceBlue; you are using the static Color Enumeration in System.Drawing.

For many people new to programming in C#, it is a little hard to get used to the idea that if you define a static method, function, or variable of some Type, in a Form, or UserControl, that what you have defined then becomes a method, function, or variable, of the Form or UserControl. No matter how many instances of the Form, or UserControl, are created, there will be one, and only one, instance of the variable, method, or function.

The most common way to have a global variable in .NET C# is to define a Static Class in the same Namespace used by the other Classes that you wish to have access to the global variable.

See if you find this answer helpful: "How to define Global veriable in C#.net:" [^].

I think the danger, for a beginning programmer, in making use of global variables, is that it may become a way to avoid the more rigorous discipline of learning how to pass data, or functions, or methods, from one object to another. And, carefully implementing the ways in which objects "talk" to each other is a key in programming for a future world where most applications are going to be multi-user, multi-threaded, client-server, etc.

So, when you say above that there's another class that should "know" about the changed value of a boolean variable in a second class: well, yes, you could create a static class that held a boolean that would be set by the second class anytime the boolean changed, and then the other class could always have access to the current value by using the static variable.

But, you could also implement that by passing a "message" from the second class to the other.

And, you could achieve that in several ways: via an Event; via exposing a public Property in the second class that the other class had access to, etc.

CodeProject's OriginalGriff (accept no imitation !) has written three excellent tutorials on how to implement message-passing in WinForms: from form to form; from form to "child" control; and, from "child" control to Form. I strongly suggest you study those: [^].

The techniques in those three articles are easily adaptable to passing data between Classes that do not have a UI (i.e., are not Forms, UserControls, etc.).


如果您在Web应用程序开发中,您可以使用会话变量

如果你在Windows应用程序中然后你可以使用静态变量
if you are in web application developement you can use session variables
and if you are in windows application then you can use static variables


嗨检查这个链接



如何在C#.net 中定义全局可验证[ ^ ]
hi check this link

How to define Global veriable in C#.net[^]


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

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