messageIn在当前上下文中不存在

Nim*_*sad -2 c# c#-4.0

我有一个代码如下所示.

我从查询字符串中获取消息.在那之后,我将把它传达给消息array(msg_arr).但是所有这些东西都在里面Page_load.

但为什么这个错误表明了?

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        try
        {
            string messageIn = Request.QueryString["msg"];  
            // some code here
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }

        string[] msg_arr = messageIn.Split(' '); // error shown here
        int size = msg_arr.Length;

        if(!CheckUserExistAndReporter("messageIn"))
        {
           // Response.Redirect("~/test.aspx");
        }
        else
        {
Run Code Online (Sandbox Code Playgroud)

slo*_*oth 6

messageIn 在try-block中声明了,这是你的问题.

只需在外面宣布:

string messageIn = null;
try
{
    messageIn = Request.QueryString["msg"];
    // some code here
}

...
Run Code Online (Sandbox Code Playgroud)

try嵌段创建一个新的范围,所以它里面声明的变量之外是不可见的.