Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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# - Fatal编程技术网

C# 对方法结果的即席访问

C# 对方法结果的即席访问,c#,C#,我需要CallerMethod()来访问在CalledMethod()中找到的值。 字符串s是之前独立执行的其他方法的执行结果。 CallerMethod()方法未绑定到CalledMethod()的执行。 CallerMethod()和CalledMethod()方法属于两个不同的项目。创建一个静态类来存储这个值似乎是不对的。你认为什么是最好的方法? 这些项目是Windows窗体 Public void PreviousMethod() { CalledMethod(a); } Pu

我需要CallerMethod()来访问在CalledMethod()中找到的值。 字符串s是之前独立执行的其他方法的执行结果。 CallerMethod()方法未绑定到CalledMethod()的执行。 CallerMethod()和CalledMethod()方法属于两个不同的项目。创建一个静态类来存储这个值似乎是不对的。你认为什么是最好的方法? 这些项目是Windows窗体

Public void PreviousMethod()
{
    CalledMethod(a);
}

Public void CalledMethod(string value)
{
    string s = value;
}

Public void CallerMethod()
{
    string getStringSFromCalledMethod = "?";
}

不需要进入静态变量。您只需要一个实例属性成员来存储以前计算的值

class Class1 {
    public string ValueOfImportance { get; private set; }
    public void CalledMethod(string value) {
        ValueOfImportance = value; // possibly after processing the data from other calls
    }

}

class Class2 {
    public void CallerMethod() {
        Class1 obj; // you need to have the object reference of Class1
        string getStringSFromCalledMethod = obj.ValueOfImportance;
    }
}

当您生成
ValueOfImportance
时,需要确保对象引用可用于
Class2.CallerMethod()

为什么需要创建静态类?价值从何而来?这是“神奇的价值”吗?