Solved

Null check on Convert.ToInt32

Posted on 2009-05-11
8
910 Views
Last Modified: 2012-08-13
I am getting a null expection on
Convert.ToInt32(orgValue);
What is the best way to overcome this?
0
Comment
Question by:tbaseflug
8 Comments
 
LVL 3

Accepted Solution

by:
xbrady earned 500 total points
ID: 24355743
I'm not sure if I know exactly what you are looking for but you can replace the code you listed with this code:
(orgValue== null) ? 0 : Convert.ToInt32(orgValue);

That will return 0 if the value is null, or return the converted value if it is not null.
0
 
LVL 5

Expert Comment

by:Buginator
ID: 24355816
Agree with xbrady. I'm posting an practical example how you can use it in case you are unsure:

int newValue = (orgValue== null) ? 0 : Convert.ToInt32(orgValue);
0
 
LVL 8

Expert Comment

by:ppittle
ID: 24355885
Personal preference:

int orgValueAsInt;
if (!Int.TryParse((orgValue as string), out orgValueAsInt)
{
//Parse Failed.  Assign orgValueAsInt a default value?
orgValueAsInt = -1;
}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Expert Comment

by:OblivionSY
ID: 24355949
ppittle's TryParse is the ideal option, it's what it is made for :)
0
 
LVL 3

Expert Comment

by:steeza
ID: 24356144
Or use the null coalescing operator:

Convert.ToInt32(orgValue ?? 0);

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q__24398343.html

So pretty ;)
0
 
LVL 3

Expert Comment

by:steeza
ID: 24356233
or "0" or whichever, you get the point :)
0
 
LVL 20

Expert Comment

by:informaniac
ID: 24362325
On second thoughts

doesn't Convert handle nulls by itself? That's why it is recommended to use Convert instead of int.Parse?

http://anuviswan.blogspot.com/2006/03/intparse-vs-converttoint32.html

http://blogs.msdn.com/ianhu/archive/2005/12/19/505702.aspx
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 24362691
It does say that, yes, MSDN would seem to support that too, interesting - What is the Type of orgValue in Convert.ToInt32(orgValue);?

Article does show that the new try parse is much faster if there are conversions that will fail.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question