我在这行代码中有一些问题:
if(String.IsNullOrEmpty(m_nameList[index]))
我做错了什么?
编辑:在VisualStudio中,m_nameList带有红色下划线,并且说“在当前上下文中不存在名称”m_nameList“?
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]; } .... }