2014-06-29 232 views
0

我試圖將Button1單擊事件中生成的變量的值傳遞給另一個函數(function2)。什麼是最有效的方法來做到這一點?會話對象?將一個函數的值傳遞給另一個函數

private static string createAuthCode() 
{ 
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); 
    byte[] buff = new byte[5]; 
    rng.GetBytes(buff); 
    return Convert.ToBase64String(buff); 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    //code 
    //.. 
    try { 
    ... 
    string authCode = createAuthCode(); 
    } 
    //code 
    //.. 
    function2(); 
} 

protected void function2() 
{ 
    //I want to access authCode here 
} 
+6

你不能更改'function2'的簽名嗎?如果'function2'只能從'Button1_Click'被調用,那麼爲什麼不把它作爲參數傳遞 –

回答

4

爲什麼不使用像Manish Mishra這樣的參數?

在的button1_Click,調用函數2這樣

function2(authCode); 

你的函數2改成這樣:

protected void function2(String authCode) 
{ 
    //access authCode here 
} 
2

如果參數傳遞給後續的方法調用是不是一種選擇,你應該使用HttpContext.Current.Items。這是一個內存中的對象集合,一旦請求結束(或換句話說,它是一個請求存儲),它就會持續存在。

相關問題