简体   繁体   English

无法在静态字段中设置变量值

[英]Can't Set variable value in Static Field

In this simple example Why StrA in Sample() function Can't set with "Hi World" string ? 在这个简单的示例中,为什么Sample()函数中的StrA无法设置为“ Hi World”字符串?

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());
}

make StrA a static field, right now it's just a local property of the instance, so when you create a new instance with MyClass1 = new MyClass(); 将StrA设置为静态字段,现在它只是实例的本地属性,因此当您使用MyClass1 = new MyClass()创建新实例时;

the StrA property is empty (null) and even if you set it a value, after you create a new instance that new instance will have SrtA as empty... StrA属性为空(空),即使您将其设置为一个值,在创建新实例后,新实例也会将SrtA设置为空...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM