微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 检查数组是空还是空?

在这代码中有一些问题:
if(String.IsNullOrEmpty(m_nameList[index]))

我做错了什么?

编辑:在VisualStudio中,m_nameList带有红色下划线,并且说“在当前上下文中不存在名称”m_nameList“?

编辑2:我添加了一些更多的代码

class SeatManager
{
    // Fields
    private readonly int m_totNumOfSeats;

    // Constructor
    public SeatManager(int maxnumOfSeats)
    {
        m_totNumOfSeats = maxnumOfSeats;

        // Create arrays for name and price
        string[] m_nameList = new string[m_totNumOfSeats];
        double[] m_priceList = new double[m_totNumOfSeats];
    }

    public int GetNumReserved()
    {
        int totalAmountReserved = 0;

        for (int index = 0; index <= m_totNumOfSeats; index++)
        {
            if (String.IsNullOrEmpty(m_nameList[index]))
            {
                totalAmountReserved++;
            }
        }
        return totalAmountReserved;
    }
  }
}

解决方法

之后编辑2:

您将m_nameList定义为构造函数的局部变量.
您的其余代码需要它作为一个字段:

class SeatManager
{       
   // Fields
   private readonly int m_totNumOfSeats;
   private string[] m_nameList;
   private double[] m_priceList;

  // Constructor
  public SeatManager(int maxnumOfSeats)
  {
     m_totNumOfSeats = maxnumOfSeats;

     // Create arrays for name and price
     m_nameList = new string[m_totNumOfSeats];
     m_priceList = new double[m_totNumOfSeats];
  }

  ....
}

原文地址:https://www.jb51.cc/csharp/97152.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐