2

I am developing a windows phone 7 app and need to declare variables as global so that the information they carry can be used anywhere in the application.

I tried this by declaring variables as public static. I tried almost all the possibilities i could think of but none worked. can we make variables global? If yes, how?

4

2 回答 2

2

如果你想要一个全局变量,public static那就是你需要的。但是,您应该更喜欢使用属性而不是公共变量:

 public class GlobalData {
     private static string someString = "Hello, world!";
     public static string SomeString {
         get {return someString;}
         set {someString = value;}
     }
 }

您可以使用完全限定名称来引用此全局变量,如下所示:

GlobalData.SomeString = "Quick brown fox";
Console.WriteLine("Global variable value is '{0}'", GlobalData.SomeString);
于 2013-07-24T18:18:04.523 回答
0

您是否尝试将全局变量放在公共类中并通过类名而不是变量名引用它们?

public static class GlobalVariables {
    public static int MrGlobalInt = 5;
}
...
GlobalVariables.MrGlobalInt = 10;
于 2013-07-24T18:17:59.933 回答