2012-11-30 18 views
-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餅乾從public bool od_auth(string login, string pass)。 我該怎麼辦呢

+0

下一次,你應該告訴你嘗試一下,因爲我們不希望做你的工作)。 – looper

回答

2

不要在這種情況下使用var,使餅乾場:

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

     } 
    } 
} 
+0

'cookies'不是* global *。它是類的成員變量/字段。 – sloth

+0

@DominicKexel:是的,剛發佈後就已經看過了,已經編輯過了。但謝謝:)。 – looper

+1

你的答案當然是對的,每當我聽到C#中的*全局變量*時,它就會讓我感到困惑。 – sloth

4

您只需使用一個字段或屬性:

Fields

private CookieType cookie; 

Properties

private CookieType cookie { get; set; } 

值將成爲內可見對象

0

只需將變量移動到類級別並在類中聲明它們,但在表單的構造函數之前。然後你班上的所有方法都可以訪問它們。