加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:50:59 所属栏目:百科 来源:网络整理
导读:我在这行代码中有一些问题: if(String.IsNullOrEmpty(m_nameList[index])) 我做错了什么? 编辑:在VisualStudio中,m_nameList带有红色下划线,并且说“在当前上下文中不存在名称”m_nameList“? 编辑2:我添加了一些更多的代码 class SeatManager{ // Fiel
我在这行代码中有一些问题:
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];
  }

  ....
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读