Link to home
Start Free TrialLog in
Avatar of dirtdart
dirtdart

asked on

Null references

I have created a C# class for use in a program.  In this class, I have overloaded the equals operator so that it compares the Name property of the class to declare equality.  The problem comes in when I then try to determine if an instance of that class is null.  The execution is passed to the overloaded equals function, but since one or both sides is null, then a System.NullReferenceException is thrown.  I am currently currently using the following method as a workaround, but I know there has to be a better way to handle this:
public static bool operator==(ConfigSection left, ConfigSection right)
{
                 try
      {
            if((left.Name == right.Name) && (left.Type == right.Type))
                  return true;
      }
      catch(System.NullReferenceException nr)
      {
            return true;
      }

      return false;
}

Can anyone tell me a better way to do this?
Avatar of smitty22
smitty22

What if you test for null in an if statement, before you test the Name and Type properties?

If one of the objs is null, but the other one is not, then the objs are obviously not equal, so return false.  I would probably also return false in the case where both objs are null.


public static bool operator ==( ConfigSection left, ConfigSection right ) {
  if ( left == null || right == null ) {
    return false;
  }

  return left.Name == right.Name && left.Type == right.Type;
}
Avatar of dirtdart

ASKER

I tried that.  It calls the overloaded equals operator again, which performs the test again, which calls the equals operator again...  And eventually leads to a stack overflow.
then it would appear that your best choice is your first approach---catch the NullException when it occurs.

but rather than returning 'true' on the NullException (as your code now does), return 'false', or are you allowing null to equal ANYTHING.  Seems that null should equal NOTHING, hence a null should always trigger a false return.

AW
The reason I am having the exception return true is the following code:

if(cs == null)

This will enter the overloaded operator.  If either side is null, it will throw an exception.  This doesn't return the correct response, but if I were to return false, then I would never know if the object were equal to null.  The more I think about it, I'm beginning to think that my best option is going to be not comparing it to null at all, but simply trying to use the object and catching the System.NullReferenceException caused by that.  Anyone with any other suggestions?
ASKER CERTIFIED SOLUTION
Avatar of tinchos
tinchos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  That wasn't what I was hoping for, but it's definately the right answer.  I guess that's one of the few spots where I still favor C++ over C#.
Yeap, that happens very often.........

I felt the same way you're feeling now after 5 hours trying to figure out how I could solve it.

Cheers

Tincho
- Use the static Equals method of Object() to check for equality of both references and to check for equality to null
- of course you should also overload!= operator

class ConfigSection
{
string name;

            public static bool operator == (ConfigSection left, ConfigSection right)
            {
                  if(Object.Equals(left,right))
                        return true;

                  if(Object.Equals(left,null) || Object.Equals(right,null))
                        return false;
                  return left.name.Equals(right.name);
            }

            public static bool operator != (ConfigSection left, ConfigSection right)
            {
                  return !(left == right);
            }

            public ConfigSection(string s)
            {
                  name = s;
            }
      }
That's great, valentint!  As easy as C# is, I still seem to have quite a bit of learning to do.  I'll post another question to award you points, because that's exactly what I was looking for.