我有一个类库项目,我想创建一个变量并通过MethodA()
给它分配一个值,该变量的值可以通过MethodB()
进行访问。
就像我们在ASP.NET
中开会一样。
我无法将参数作为参数传递给MethodB()
,因为MethodB()
在许多地方都在使用,如果更改它,其他所有方法都会受到影响。
Public Void MethodA()
{
string value ="Hello";
}
public Void MethodB()
{
-- I want to read the value which is set in MethodA()
}
在MethodB()
中读取值后,我还需要处理该值。
这两种方法都在同一项目的不同类中。
答案 0 :(得分:0)
首先尝试使用私有设置器创建属性:
public class A
{
public string Value { get { return MethodA(); } }
public string MethodA()
{
return "Hello";
}
public void MethodB()
{
var value = Value;
}
}
如果您有两个班级:
public class A
{
public string FooMethod()
{
return string.Empty;
}
}
public class B
{
public string BarMethod()
{
var result = new A().FooMethod();
return result;
}
}
答案 1 :(得分:0)
您有主意来处理您的异常
public class test
{
public static int value = 0; //Global variable
public void MethodA()
{
//you can assign here as well
value++;
}
}
public class HomeController : Controller
{
test t = new test();
public IActionResult Index()
{
t.MethodA();
int d = test.value;
//you can assign here as well
test.value = 100;
int dd = test.value;
return View();
}
}