Solved

Null check on Convert.ToInt32

Posted on 2009-05-11
8
912 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
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.

 
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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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