在方法之间共享c#变量?

时间:2014-10-14 17:10:43

标签: c#

我有不同的方法如何在它们之间共享strHeaderFileName(等)变量? 例如,我需要在btnSave_Click方法中使用strHeaderFileName

protected void multiFileUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    // No Loop needed multiFileUpload_FileUploaded will be called for each file uploaded
    var strHeaderFileName = e.File.FileName;
    // Use the filename as the Header Name
    var strHeaderName = strHeaderFileName.Replace(".jpg", "");
    // allow Underscore characters in FileName to become Spaces in the Display Name.
    strHeaderName = strHeaderName.Replace("_", " ");
}

protected void btnSave_Click(object sender, EventArgs e)
{
    // use the same strHeaderFileName value here?
}

4 个答案:

答案 0 :(得分:4)

有几种选择。由于这些方法是事件处理程序而不是直接调用,因此这里的最佳方法可能是变量类级成员:

private string strHeaderFileName;

protected void multiFileUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    strHeaderFileName = e.File.FileName;
    // etc.
}

protected void btnSave_Click(object sender, EventArgs e)
{
    // here you can access strHeaderFileName
}

作为类级别成员,可以通过该类中的任何方法访问它们。 (确保你保留它们private或其他对象也能够访问它们,在这种情况下你可能不想要它们。)并且它们将在该类的任何给定实例的生命周期中存在。 / p>

还要注意这看起来像ASP.NET,它在类实例方面的行为与WinForms之类的行为略有不同。 Page的任何给定实例都不会在请求之间保留。因此,如果您在一个处理程序中设置值,再次显示该页面,然后再调用另一个处理程序,该值将不再存在。这是因为每个Page的类实例是按请求创建的,然后在响应后销毁。

要保持跨页面请求,您需要将值保留在其他位置。例如,如果它需要在该用户会话的范围内生存,那么您可以将其置于会话状态:

protected void multiFileUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    Session["strHeaderFileName"] = e.File.FileName;
    // etc.
}

protected void btnSave_Click(object sender, EventArgs e)
{
    // here you can access Session["strHeaderFileName"]
}

根据值需要持久化的范围,您还可以将其放在页面上,cookie中,数据库中,某些缓存机制中等。

答案 1 :(得分:2)

在这种情况下,最合适的选项似乎是:使用实例字段:

string strHeaderFileName;

protected void multiFileUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    // ...
    strHeaderFileName = e.File.FileName;
    // ...
}

protected void btnSave_Click(object sender, EventArgs e)
{
    // can use strHeaderFileName
}

答案 2 :(得分:2)

你可以使它们成为成员变量而不是局部变量,例如

string strHeaderFileName;
string strHeaderName;

    protected void multiFileUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    // No Loop needed multiFileUpload_FileUploaded will be called for each file uploaded
    strHeaderFileName = e.File.FileName;
    // Use the filename as the Header Name
    strHeaderName = strHeaderFileName.Replace(".jpg", "");
    // allow Underscore characters in FileName to become Spaces in the Display Name.
    strHeaderName = strHeaderName.Replace("_", " ");
}

protected void btnSave_Click(object sender, EventArgs e)
{
    // strHeaderFileName is now accessible here
}

答案 3 :(得分:2)

var strHeaderFileName声明为您班级的成员。 所以,如果这是一个页面,例如

class Page
{
  private string strHeaderFileName;
 protected void multiFileUpload_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        // No Loop needed multiFileUpload_FileUploaded will be called for each file uploaded
        strHeaderFileName = e.File.FileName;
        // Use the filename as the Header Name
        var strHeaderName = strHeaderFileName.Replace(".jpg", "");
        // allow Underscore characters in FileName to become Spaces in the Display Name.
        strHeaderName = strHeaderName.Replace("_", " ");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        // use the same strHeaderFileName value here?
       someFunctionUses(strHeaderFileName);
    }
}