在我的aspx.cs页面中,我有一个int静态变量,并将其传递给另一个类的构造函数,其中我有一个函数可以增加静态变量。然而,当执行完成时,当我回到我的aspx.cs页面我失去了我的静态变量值。清除通过构造函数传递的C#静态变量
aspx.cs页面
public static int one;
//Im creating an object to test class
Test t = new Test(ref int one);
t.Increment();
测试Class.cs
public class Test {
int _one;
public Test(ref one) {
this._one = one;
}
public void Increment() {
_one++;
}
}
编辑
我有两个aspx.cs页面保存静态变量和测试类,它有递增函数,递增aspx.cs类的静态变量。当我创建一个Test类的对象但没有用时,我尝试通过ref传递。有人能为此提出最佳设计方案吗?
one.aspx.cs
public static int one;
//Im creating an object to test class
Test t = new Test(ref int one);
t.Increment();
Two.aspx.cs
public static int Two;
//Im creating an object to test class
Test t = new Test(ref int Two);
t.Increment();
测试类
public class Test {
int _value;
public Test(ref one) {
this._value = one;
}
public void Increment() {
_value++;
}
}
你没有在你的'Test'类(你不能)持有一个引用(指针)到静态变量'one'。 – EZI
不知道为什么这个问题应该得到一个downvote? – Rahul
您的技术在ASP.NET中无法使用。如果你可以增加静态变量,你可以为页面的所有用户增加它。 –