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;
    }
  }
}
2
  • 2
    What kind of problem? Do you get IndexOutOfRangeException? Post your error Commented Apr 7, 2012 at 8:17
  • thats write marlon...take back my comment Commented Apr 7, 2012 at 8:20

3 Answers 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));
Sign up to request clarification or add additional context in comments.

2 Comments

@Downvoter: Care to comment? Bear in mind this answer was written long before the OP included his code...
Thanks for the help! Yes, the code is as it is now the the wrong way raound! I will fix it!
6

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

  ....
}

2 Comments

Great! I missed that! Thanks for the help! :)
@3D-kreativ: Note that that's only one of the problems in the code - see my answer for another.
4

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

8 Comments

Nice. Lots of tests there... I count seven? No, eight! Makes me think exceptions sure are wonderful!
The purpose of this is avoiding the exception, it is not supposed to be faster then get the exception ...
@KevinP.Rice: I would never just let the exception come through and catch it here. An ArrayIndexOutOfBounds exception should always indicate a bug, not something deliberate. And I count four tests (all OR-ed together, of course), not eight. There are 4 conditions being checked here, and all are valid to check. Were you counting each compound condition as an extra one?
@KevinP.Rice: I think anyone who wasn't trying to get to the maximum possible number here would count this as four conditions. I think given that the title of the question is "Check if array is null or empty?" it's reasonable to assume that the OP really did want to perform a check. And sure, letting an exception bubble up might be fine - but that's not what you were suggesting. You were suggesting catching the exception: "If rare, then try/catch is much better". I definitely disagree with that. Catching ArrayIndexOutOfBoundsException or NullReferenceException is simply a bad idea.
@KevinP.Rice: Not tonight, but I'll mail myself to look into it tomorrow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.