Closed Thread
Results 1 to 4 of 4

Thread: Check object for null

  1. #1
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30

    Check object for null

    I was looking at a standards website and found this code:

    Code:
    using System.Diagnostics;
    
    object GetObject()
    {...}
    
    object someObject = GetObject();
    Debug.Assert(someObject != null);
    Which seemed very odd to me. I always check for null like

    Code:
    if (!someObject != null)
    but here they use Debug.Assert. Does that seem odd or normal to you?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    brackett is offline Programmer
    Join Date
    May 2006
    Posts
    192
    Rep Power
    22
    The sample code isn't actually checking for null - they're asserting it. An assert only fires under Debug builds (technically, when the DEBUG compiler constant is defined) and displays a message/outputs to any listeners a callstack if the Assert fails. This can aid in debugging.

    In Release builds, the assert should fail but will really have no behaivor (because the DEBUG constant is not defined). Note that the Visual C++ and JScript.NET compilers ignore this, and have the same behaivor in both Release and Debug.

  4. #3
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30
    Ok, I understand now. What does this assert do?

    Say

    Code:
    object someObject = GetObject();
    someObject = null;
    Debug.Assert(someObject != null);
    What will display on the debug window?

  5. #4
    brackett is offline Programmer
    Join Date
    May 2006
    Posts
    192
    Rep Power
    22
    By default, in a Debug build, that'll show a message box (in a UI app) and a stack trace.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. NULL
    By BASHERS33 in forum Database & Database Programming
    Replies: 23
    Last Post: 05-27-2009, 02:45 AM
  2. How do I check whether an object pointer is deleted?
    By MerakSpielman in forum C and C++
    Replies: 6
    Last Post: 08-06-2008, 08:02 PM
  3. Object is Null
    By Lop in forum C# Programming
    Replies: 2
    Last Post: 09-21-2006, 09:25 AM
  4. Best way to check if an object is null?
    By dirkfirst in forum Managed C++
    Replies: 0
    Last Post: 08-01-2006, 05:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

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