Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个类获取公共变量_C#_Variables_Xamarin.forms - Fatal编程技术网

C# 从另一个类获取公共变量

C# 从另一个类获取公共变量,c#,variables,xamarin.forms,C#,Variables,Xamarin.forms,我有一个类,其中有一个公共静态变量,我想从另一个类访问它。下面是课程: 在GlobalVariable.cs中 public class GlobalVariable { public static int MoisVS = 3; } 我想从另一个类访问“MoisVS”。下面是课程: 在ArretPage.cs中 var globalvariable = new ProjetTN.Class.GlobalVariable(); globalvariable.MoisVS; //<

我有一个类,其中有一个公共静态变量,我想从另一个类访问它。下面是课程: 在GlobalVariable.cs中

public class GlobalVariable
{
    public static int MoisVS = 3;
}
我想从另一个类访问“MoisVS”。下面是课程: 在ArretPage.cs中

var globalvariable = new ProjetTN.Class.GlobalVariable();
globalvariable.MoisVS;  //<--- Make something like that.
var globalvariable=new ProjetTN.Class.globalvariable();

全局变量// 您将
MoisVS
定义为静态,因此无法从实例变量访问它。访问使用类名本身的类的静态成员。例如,访问您的
moisv
将如下所示:

GlobalVariable.MoisVS
如果要将其作为实例属性访问,则必须将类更改为:

public class GlobalVariable
{
    public int MoisVS = 3;
}
如果类中只有静态值或常量值。您还可以决定使整个类保持静态

public static class GlobalVariable
{
    public static int MoisVS = 3;
    public const string MyString = "";
}

这将阻止您使用new关键字创建该类的实例。

我将把它分成几个步骤

1) 文件->新建文件->常规->选择空类->命名它(例如UICONTROL\u名称)

2) 添加以下内容:(示例代码)

请注意:

UICONTROL_NAMES class is inside a folder group called HelperClass and iOSControls is the project name.
3) 在ViewController.cs上,添加名称空间指令

using iOSControls.HelperClass;
using Foundation;
4) 访问UICONTROL名称中的常量字符串:

string controlName = UICONTROL_NAMES.textField;

你为什么不试试?@MathiasR.Jessen我试过了,但没有成功,这就是为什么我问这个问题:)
string controlName = UICONTROL_NAMES.textField;