-5

大家好,有代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public bool od_auth(string login, string pass)
        {
            var cook­ies = 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()"var cookie 中使用public bool od_auth(string login, string pass)。我该怎么做?

4

3 回答 3

4

您可以简单地使用字段或属性:

领域

private CookieType cookie;

属性

private CookieType cookie { get; set; }

该值将在对象内部可见

于 2012-11-30T14:30:21.063 回答
2

不要var在这种情况下使用并将 cookie 设为字段:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private CookieContainer cookies;

        public Form1()
        {
            InitializeComponent();
        }

        public bool od_auth(string login, string pass)
        {
            cook­ies = 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)
        {

        }
    }
}
于 2012-11-30T14:31:01.190 回答
0

只需将变量移至类级别并在类中声明它们,但在表单的构造函数之前。然后,您班级中的所有方法都可以访问它们。

于 2012-11-30T14:30:55.180 回答