Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if array is null or empty?

Tags:

c#

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;
    }
  }
}
like image 620
3D-kreativ Avatar asked Apr 07 '12 08:04

3D-kreativ


People also ask

How do you check if an array is empty or null?

To check if an array is empty or not, you can use the .length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not. An empty array will have 0 elements inside of it.

How do you check if the array is empty or not in Java?

The isEmpty() method of ArrayList in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.

How check if string is null array?

Example 1 – Check if Array is Empty using Null Check To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null.

How check if array is empty C#?

To check if an given array is empty or not, we can use the built-in Array. Length property in C#. Alternatively, we can also use the Array. Length property to check if a array is null or empty in C#.


3 Answers

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));
like image 90
Jon Skeet Avatar answered Sep 25 '22 01:09

Jon Skeet


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

  ....
}
like image 43
Henk Holterman Avatar answered Sep 26 '22 01:09

Henk Holterman


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 ...

like image 24
aleroot Avatar answered Sep 24 '22 01:09

aleroot