无法在静态字段中设置变量值
Can't Set variable value in Static Field
在这个简单的例子中为什么StrA
in Sample()
function Can't set with "Hi World" string ?
string StrA { get; set; }
private void button1(object sender, EventArgs e)
{
StrA = "Hi World"; //=======> Get StrA value
}
public static string Sample()
{
MyClass MyClass1 = new MyClass();
string a = MyClass1.StrA; //==========> Can't Set StrA value with "Hi World" string ???
return (MessageBox.Show(a).ToString());
}
使 StrA 成为静态字段,现在它只是实例的本地 属性,因此当您使用
MyClass1 = new MyClass();
StrA 属性 为空 (null),即使您将其设置为一个值,在您创建新实例后,新实例的 SrtA 也会为空...
在这个简单的例子中为什么StrA
in Sample()
function Can't set with "Hi World" string ?
string StrA { get; set; }
private void button1(object sender, EventArgs e)
{
StrA = "Hi World"; //=======> Get StrA value
}
public static string Sample()
{
MyClass MyClass1 = new MyClass();
string a = MyClass1.StrA; //==========> Can't Set StrA value with "Hi World" string ???
return (MessageBox.Show(a).ToString());
}
使 StrA 成为静态字段,现在它只是实例的本地 属性,因此当您使用 MyClass1 = new MyClass();
StrA 属性 为空 (null),即使您将其设置为一个值,在您创建新实例后,新实例的 SrtA 也会为空...