testing if an object is null
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: testing if an object is null

  1. #1
    Join Date
    Jan 2009
    Posts
    10

    testing if an object is null

    How can I test to see if an object is null.
    Everything I've found says I should use:
    Code:
    if (myObject == null)
    Unfortunately I've overloaded the == operator so this won't work. Is there another way to do this or something I could do to my overloaded operator to handle this situation?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: testing if an object is null

    Why wouldn't that work? If your overload cannot handle a comparison to null then you need to fix it. I assume that you overrode .Equals() as well, right?
    Last edited by BigEd781; February 18th, 2009 at 05:56 PM.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: testing if an object is null

    Then your overloaded operator should do a nullcheck before it does the usual equality check.

    Alternatively if you cast to 'object' first, you'll use the regular (non-overloaded) comparison:

    Code:
    if ((object)myObject == null) {
        Console.WriteLine ("I'm using the regular '==' and i'm null");
    }
    EDIT: Generally speaking you should only override == for immutable types. So if your type is mutable, then don't override '=='. If you want value comparison, you should be overriding the 'Equals' method.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: testing if an object is null

    Quote Originally Posted by Mutant_Fruit View Post
    EDIT: Generally speaking you should only override == for immutable types. So if your type is mutable, then don't override '=='. If you want value comparison, you should be overriding the 'Equals' method.
    +1 rep if I could.

  5. #5
    Join Date
    Jan 2009
    Posts
    10

    Re: testing if an object is null

    Unfortunately this is a school project so the choice to use .Equals() as opposed to == is not my choice to make. I think the cast will work. I hadn't thought of that. Thanks.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: testing if an object is null

    Quote Originally Posted by afireohno View Post
    Unfortunately this is a school project so the choice to use .Equals() as opposed to == is not my choice to make. I think the cast will work. I hadn't thought of that. Thanks.
    You should let your teacher know that they are not really teaching this concept in the best way.

  7. #7
    Join Date
    Jan 2009
    Posts
    10

    Re: testing if an object is null

    Sometimes I think they give assignments with ridiculous specifications just to confuse and weed people out of the program.

  8. #8
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: testing if an object is null

    Quote Originally Posted by afireohno View Post
    Sometimes I think they give assignments with ridiculous specifications just to confuse and weed people out of the program.
    Agreed

    Instructors tend to make you do things in awkward ways, perhaps its because they dont fully understand the concepts. Or because thats the only way they know how and they can grade successfully. My instructor for VB.NET (sorry... but ick) makes us write comments on all of out methods and properties like so:
    Code:
    
    ' ====================================
    ' === MyMethod(ByVal myNameString As String) 
    ' === myNameString: The users name.
    ' === Purpose: Does something with a name.
    ' ====================================
    
    Personally I think its gaudy, so I came to him one day and told him about auto-generated XML Summary tags and he liked it and changed his curriculum a bit. I told him Ive been using C# for about 3 - 4 years now and Ive discovered alot of tricks that will make everyones life a little easier, lol.

    Ok, Im done hijacking this thread
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  9. #9
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: testing if an object is null

    Use static Object.ReferenceEquals() method:
    Code:
    if (Object.ReferenceEquals(myObject, null))
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center