[英]using a global variable on C#
So I have to use this void
所以我必须用这个void
static void PerformOption(int option)
However I want this int
option to be able to be used by other void
s as well. 但是我希望这个int
选项也可以被其他void
使用。 I know this could be done by deleting inside of brackets and assigning a static int
but I have to use int
option for this void
, so I cannot change this one. 我知道,这可以通过删除括号内和分配进行static int
,但我必须使用int
选项这个void
,所以我无法改变这一点。
So how can I make option as global variable? 那么,如何使选项成为全局变量呢?
public class MyGlobalsBecauseImEvilAndLoveBadProgrammingMemes
{
public static int Spaghetti { get; set; }
}
then in PerformOption do 然后在PerformOption中
MyGlobalsBecauseImEvilAndLoveBadProgrammingMemes.Spaghetti = option;
but hopefully you want a member, then do 但希望您想要一个会员,然后做
public class MyClass
{
int Option { get; set; }
public void PerformOption(int option)
{
Option = option
// other stuff
}
public void SomethingElse()
{
if(Option == 1) // use Option at will
{
}
}
}
Declare a global int
variable to hold option
value inside the PerformOption
method to be used in other methods. 声明一个全局int
变量,以在PerformOption
方法内保存option
值,以供其他方法使用。
public static class MyClass
{
private static int globalOption;
public static void PerformOption(int option)
{
globalOption = option;
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.