3D-kreativ
3D-kreativ

Reputation: 9297

Check if array is null or empty?

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

Upvotes: 9

Views: 99831

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1500515

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));

Upvotes: 18

Henk Holterman
Henk Holterman

Reputation: 273244

After Edit2:

You are defining m_nameList as a local variable of the constructor.
The rest of your code needs it as a field:

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

  ....
}

Upvotes: 6

aleroot
aleroot

Reputation: 72636

To avoid the error you can perform some pre conditions in the if, like these :

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

This should works fine(without causing error) in almost any conditions ...

Upvotes: 4

Related Questions