Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Correct way to check for null objects?
 

Correct way to check for null objects?

Hi all,
I'm using the Precedents object of an Excel Range:

Excel.Range range = (Excel.Range)Globals.ThisAddIn.Application.ActiveCell;
foreach (Excel.Range r in range.Precedents)
                {
                    //stuff
                }

When there are precedents, this is fine.  However if there are no precedents, I get a COMException.  I'd like to do a check to prevent the exception, but "normal" checks for null/empty objects don't work, such as:
if (range.Precedents == null)
if (range.Precedents.Count > 0)

These throw the same COMException.  It seems you can't "touch" a null/empty object at all.

Placing everything inside a try/catch works, but this feels like the wrong way to be coding, is there a better option?
Thanks...

Jonathan S.  Thursday, October 15, 2009 9:40 PM

Hello Jonathan,

If the range has not precedents, it will throw an exception when accessing this property. Here, it is right way to use try/catch block. Code like this:
try
{
foreach (Excel.Range r in range.Precedents)
{
......
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

If you have any further question, please feel free to let us know.

Best regards,
Bessie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bessie Zhao  Monday, October 19, 2009 5:21 AM
Are you checking to make sure range is not null as well?

William Wegerson (www.OmegaCoder.Com)
OmegaMan  Thursday, October 15, 2009 10:33 PM
The range is not null, even in the case of a new worksheet/blank cell.

 if (range != null)
            {
              //this executes
            }
Jonathan S.  Thursday, October 15, 2009 11:24 PM

Hello Jonathan,

If the range has not precedents, it will throw an exception when accessing this property. Here, it is right way to use try/catch block. Code like this:
try
{
foreach (Excel.Range r in range.Precedents)
{
......
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

If you have any further question, please feel free to let us know.

Best regards,
Bessie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bessie Zhao  Monday, October 19, 2009 5:21 AM
This is the fastest option, and you can check that the error message returned is "No cells were found."

There is a much slower way of doing this, and that is to
- ShowPrecendents
- NavigateArrows

cheers
incre-d  21 hours 57 minutes ago
Hi all,
Thanks for the replies. I've implemented the try/catch(s) and they work fine. Was hoping for something that made the code "cleaner", but if that's the way it is, that's the way it is...
-Jon
Jonathan S.  10 hours 6 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Intercepting SAVE process in Powerpoint 2007 & Word 2007
• Vista?
• ClickOnce (VS 2008 + .net 3.5) Outlook 2007 add-in manifest: How to set <friendlyName>
• Programmatically set Tools --> Options --> View --> Bookmarks property in Word
• trying to debug COMException
• Still in need for help...
• Calling VBA Function from VSTO Code
• Outlook Add In Kontact Form
• Sync Outlook contacts from a database thru a server process
• Live Preview of Table in the user control