大家好,有个代码:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool od_auth(string login, string pass)
{
var cookies = new CookieContainer();
request.CookieContainer = cookies;
if (response.Headers["Location"] != null)
{
return true;
}
else
{
return false;
}
}
public bool od_info_changer()
{
request.CookieContainer = cookies;
if (response.Headers["Location"].IndexOf("st.cmd=userSettings") != -1)
{
return true;
}
else
{
return false;
}
}
private void Auth_Click(object sender, EventArgs e)
{
string login = textBox1.Text;
string pass = textBox2.Text;
bool avt = od_auth(login, pass);
bool change = od_info_changer();
if (avt == true)
{
}
else
{
}
if (change == true)
{
}
else
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
我必须在"public bool od_info_changer()"
中使用来自public bool od_auth(string login, string pass)
的var Cookie。
我该怎么办?
答案 0 :(得分:4)
您可以简单地使用字段或属性:
private CookieType cookie;
private CookieType cookie { get; set; }
该值将在对象
中显示答案 1 :(得分:2)
在这种情况下不要使用var
并将Cookie设为字段:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private CookieContainer cookies;
public Form1()
{
InitializeComponent();
}
public bool od_auth(string login, string pass)
{
cookies = new CookieContainer();
request.CookieContainer = cookies;
if (response.Headers["Location"] != null)
{
return true;
}
else
{
return false;
}
}
public bool od_info_changer()
{
request.CookieContainer = cookies;
if (response.Headers["Location"].IndexOf("st.cmd=userSettings") != -1)
{
return true;
}
else
{
return false;
}
}
private void Auth_Click(object sender, EventArgs e)
{
string login = textBox1.Text;
string pass = textBox2.Text;
bool avt = od_auth(login, pass);
bool change = od_info_changer();
if (avt == true)
{
}
else
{
}
if (change == true)
{
}
else
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
答案 2 :(得分:0)
只需将变量移动到类级别并在类中声明它们,但是在表单的构造函数之前。您的班级中的所有方法都可以访问它们。