C# 将值从一个函数传递到另一个函数
我试图将try{}中Button1 click事件中生成的变量的值传递给另一个函数(function2)。最有效的方法是什么?会话对象C# 将值从一个函数传递到另一个函数,c#,asp.net,function,session,variables,C#,Asp.net,Function,Session,Variables,我试图将try{}中Button1 click事件中生成的变量的值传递给另一个函数(function2)。最有效的方法是什么?会话对象 private static string createAuthCode() { RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] buff = new byte[5]; rng.GetBytes(buff); return Convert.
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
}
为什么不使用Manish Mishra建议的参数 在Button1\u单击中,按如下方式调用函数2
function2(authCode);
将功能2更改为:
protected void function2(String authCode)
{
//access authCode here
}
为什么不使用Manish Mishra建议的参数 在Button1\u单击中,按如下方式调用函数2
function2(authCode);
将功能2更改为:
protected void function2(String authCode)
{
//access authCode here
}
如果向后续方法调用传递参数不是一个选项,那么应该使用。它是一个内存中的对象集合,在请求结束后仍然有效(或者换句话说,它是一个每个请求的存储)。如果向后续方法调用传递参数不是一个选项,您应该使用。它是一个内存中的对象集合,在请求结束后将持续(或者换句话说,它是每个请求的存储)。您不能更改
函数2
的签名吗?如果function2
仅用于从按钮1\u单击调用,那么为什么不将其作为参数传递?您不能更改function2
的签名吗?如果function2
仅用于从按钮1\u单击调用,那么为什么不将其作为参数传递