Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-13-2006, 06:09 PM
Lop's Avatar   
Lop Lop is offline
Speaks fluent binary
 
Join Date: May 2006
Posts: 1,135
Rep Power: 16
Lop is on a distinguished road
Default 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?
__________________
Lop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-13-2006, 07:35 PM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 10
brackett is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-14-2006, 09:10 AM
Lop's Avatar   
Lop Lop is offline
Speaks fluent binary
 
Join Date: May 2006
Posts: 1,135
Rep Power: 16
Lop is on a distinguished road
Default

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?
__________________
Lop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-18-2006, 05:56 AM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 10
brackett is on a distinguished road
Default

By default, in a Debug build, that'll show a message box (in a UI app) and a stack trace.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Object IO Stream Data Loss Possible? Any solution, pls? reachpradeep Java Help 0 03-05-2007 02:02 PM
object in session.... "<logic:present>" with JSTL? reachpradeep Java Help 0 03-04-2007 08:46 AM
Object is Null Lop C# Programming 2 09-21-2006 11:25 AM
[VB.NET]WebBrowser Object Help CheeseBurgerMan Visual Basic Programming 22 08-29-2006 11:04 AM
Best way to check if an object is null? dirkfirst Managed C++ 0 08-01-2006 07:29 AM


All times are GMT -5. The time now is 02:36 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads