2014-06-29 264 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。这是一个内存中的对象集合,一旦请求结束(或换句话说,它是一个请求存储),它就会持续存在。

相关问题