如何制作常见的var?

时间:2012-11-30 14:28:21

标签: c#

大家好,有个代码:

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()"中使用来自public bool od_auth(string login, string pass)的var Cookie。 我该怎么办?

3 个答案:

答案 0 :(得分:4)

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

Fields

private CookieType cookie;

Properties

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)
        {
            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)
        {

        }
    }
}

答案 2 :(得分:0)

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