この簡単な例では、なぜ関数StrA
内Sample()
で「HiWorld」文字列で設定できないのですか?
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は空になります...
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加