在另一种方法

时间:2017-02-27 07:39:27

标签: c#

我试图在另一个方法中使用value,例如在下面的代码中我有这个值userPostedFile.FileName:

public partial class _WebForm1 : System.Web.UI.Page
{
public void Button1_Click(object sender, EventArgs e)
{
    string filepath = Server.MapPath("\\Upload");
    HttpFileCollection uploadedFiles = Request.Files;
    Span1.Text = string.Empty;

    for (int i = 0; i < uploadedFiles.Count; i++)
    {
        HttpPostedFile userPostedFile = uploadedFiles[i];

        if (userPostedFile.ContentLength > 0)
        {
            userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName));

            Span1.Text += "Location where saved: " + filepath + "\\" + Path.GetFileName(userPostedFile.FileName) + "<p>";

        }}}

我想在下面的代码中使用它,但是我收到的错误是: 名称在当前上下文中不存在:

     private int get_pageCcount(string file)
     {
    using (StreamReader sr = new StreamReader(Server.MapPath(userPostedFile.FileName)))
    {
        Regex regex = new Regex(@"/Type\s*/Page[^s]");
        MatchCollection matches = regex.Matches(sr.ReadToEnd());

        return matches.Count;

         }}}         

4 个答案:

答案 0 :(得分:1)

  

该名称在当前上下文中不存在:

上下文或范围在大括号中定义:{}。这就是变量&#34;生活&#34;并且可以找到。 userPostedFile被声明为for循环范围内的局部变量。它既不能在循环外部访问,也不能在方法之外访问! 有关详细信息,请参阅Scopes C#

在循环内部,此变量的赋值在每次迭代中都会更改:

HttpPostedFile userPostedFile = uploadedFiles[i];

因此,即使您在类级别声明变量userPostedFile,这将使其在get_pageCcount方法中可访问,也不清楚您将在该行中使用哪个实例:

using (StreamReader sr = new StreamReader(Server.MapPath(userPostedFile.FileName)))

假设您要在类的范围内声明变量,就像使用filepathuploadedFiles

一样
public partial class _WebForm1 : System.Web.UI.Page
{
    HttpPostedFile userPostedFile;

    string filepath = Server.MapPath("\\Upload");
    HttpFileCollection uploadedFiles = Request.Files;
    Span1.Text = string.Empty;

    public void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            userPostedFile = uploadedFiles[i];

现在可以在userPostedFile方法中访问变量get_pageCcount。但现在强烈依赖 时调用方法。如果在单击按钮之前调用它。您将获得NullReferenceException因为userPostedFile尚未获得任何价值。如果您在单击按钮后调用它,您将获得uploadedFiles的最后一个值,因为它将具有在循环结束时分配的值。

答案 1 :(得分:1)

您在userPostedFile.FileName方法之外调用Button1_Click。您的变量在Button1_Click内声明,因此只能在其中调用。这就是为什么当前上下文中不存在名称的原因。

您应至少将userPostedFile分配给Button1_Click之外的变量,或者仅在userPostedFile方法之外声明Button1_Click。你可以这样做:

示例:

public partial class _WebForm1 : System.Web.UI.Page
{

HttpPostedFile userPostedFile;

public void Button1_Click(object sender, EventArgs e)
{
    string filepath = Server.MapPath("\\Upload");
    HttpFileCollection uploadedFiles = Request.Files;
    Span1.Text = string.Empty;

    for (int i = 0; i < uploadedFiles.Count; i++)
    {
        userPostedFile = uploadedFiles[i];

        if (userPostedFile.ContentLength > 0)
        {
            userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName));

            Span1.Text += "Location where saved: " + filepath + "\\" + Path.GetFileName(userPostedFile.FileName) + "<p>";

        }}}

希望它有所帮助!

答案 2 :(得分:0)

 public partial class _WebForm1 : System.Web.UI.Page
{
 HttpPostedFile userPostedFile;// İf declare userPostedFile in this scope
                                // u can use another scopes but  last value
                                // in for loop                             
  public void Button1_Click(object sender, EventArgs e)
 {
  string filepath = Server.MapPath("\\Upload");
  HttpFileCollection uploadedFiles = Request.Files;
  Span1.Text = string.Empty;

for (int i = 0; i < uploadedFiles.Count; i++)
{
    userPostedFile = uploadedFiles[i];

    if (userPostedFile.ContentLength > 0)
    {
        userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName));

        Span1.Text += "Location where saved: " + filepath + "\\" + Path.GetFileName(userPostedFile.FileName) + "<p>";

    }}}

答案 3 :(得分:0)

如果方法属于同一类

,则可以使用全局变量
public partial class _WebForm1 : System.Web.UI.Page
{
     HttpPostedFile userPostedFile = null;
     public void Button1_Click(object sender, EventArgs e)
     {
          string filepath = Server.MapPath("\\Upload");
          HttpFileCollection uploadedFiles = Request.Files;
          Span1.Text = string.Empty;

          for (int i = 0; i < uploadedFiles.Count; i++)
          {
               userPostedFile = uploadedFiles[i];

现在你可以在你的方法中使用它了。

但是如果你的方法在另一个类中,你必须像这样定义这个全局变量:

 public static HttpPostedFile userPostedFile = null;

并使用此

 _WebForm1.userPostedFile.FileName
相关问题