在方法之间共享c#变量? [英] Sharing c# variables between methods?

查看:115
本文介绍了在方法之间共享c#变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have different methods how can I share the strHeaderFileName (etc) variables between them? For example I need strHeaderFileName in the btnSave_Click method.

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?
}


推荐答案

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

There are a couple of options. Since these methods are event handlers and aren't directly invoked, the best approach here is likely to make the variable class-level members:

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 ,否则其他对象也将能够访问它们,在这种情况下,您可能不希望这样做。)并且它们将存在一生。

As class-level members, they will be accessible by any method in that class. (Make sure you keep them private or other objects will be able to access them too, which you probably don't want in this case.) And they will exist for the lifetime of any given instance of that class.

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

Also note that this looks like ASP.NET, which behaves a little differently in terms of class instances than things like WinForms. Any given instance of a Page doesn't persist between requests. So if you set the value in one handler, display the page again, and then invoke another handler the value won't be there anymore. This is because the class instance for each Page is created per-request and then destroyed after the response.

To在页面请求中保持不变,则需要将值保留在其他位置。例如,如果它需要在该用户的会话范围内使用,则可以将其置于会话状态:

To persist across page requests, you'd need to keep the value somewhere else. For example, if it needs to live during the scope of that user's session then you can put it in session state:

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中,数据库中,某些缓存机制等上。

Depending on the scope in which the value needs to persist, you could also put it on the page, in a cookie, in a database, some caching mechanism, etc.

这篇关于在方法之间共享c#变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆