9

I have some problem with this line of code:

if(String.IsNullOrEmpty(m_nameList[index]))

What have I done wrong?

EDIT: The m_nameList is underlined with red color in VisualStudio, and it says "the name 'm_nameList' does not exist in the current context"??

EDIT 2: I added some more code

    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;
    }
  }
}
4

3 回答 3

18

If m_nameList is null, that will still blow up, because it will try to find the element to pass to String.IsNullOrEmpty. You'd want:

if (m_nameList == null || String.IsNullOrEmpty(m_nameList[index]))

That's also assuming that index is going to be valid if m_nameList is non-null.

Of course, this is checking if the element of an array is null or empty, or if the array reference itself is null. If you just want to check the array itself (as your title suggests) you want:

if (m_nameList == null || m_nameList.Length == 0)

EDIT: Now we can see your code, there are two problems:

  • As Henk showed in his answer, you're trying to use a local variable when you need a field
  • You're also going to get an ArrayIndexOutOfBoundsException (once you've used a field) due to this:

    for (int index = 0; index <= m_totNumOfSeats; index++)
    

    That will perform m_totNumOfSeats + 1 iterations because of your bound. You want:

    for (int index = 0; index < m_totNumOfSeats; index++)
    

    Note that m_nameList[m_totNumOfSeats] is not valid, because array indexes start at 0 in C#. So for an array of 5 elements, the valid indexes are 0, 1, 2, 3, 4.

Another option for your GetNumReserved method would be to use:

int count = 0;
foreach (string name in m_nameList)
{
    if (string.IsNullOrEmpty(name))
    {
        count++;
    }
}
return count;

Or using LINQ, it's a one-liner:

return m_nameList.Count(string.IsNullOrEmpty);

(Are you sure you haven't got it the wrong way round though? I would have thought reservations would be the ones where the name isn't null or empty, not the ones where it is null or empty.)

If it's the wrong way round, it would be this instead in LINQ:

return m_nameList.Count(name => !string.IsNullOrEmpty(name));
于 2012-04-07T08:18:26.937 回答
6

在Edit2之后:

您定义 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];
  }

  ....
}
于 2012-04-07T08:43:34.180 回答
4

为避免该错误,您可以在 if 中执行一些前置条件,如下所示:

if(m_nameList == null || index < 0 || m_nameList.Length < index || String.IsNullOrEmpty(m_nameList[index]))

这应该在几乎任何条件下都可以正常工作(不会导致错误)......

于 2012-04-07T08:24:01.643 回答