Solved

Null check on Convert.ToInt32

Posted on 2009-05-11
8
894 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
Comment Utility
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
Comment Utility
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
Comment Utility
Personal preference:

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

Expert Comment

by:OblivionSY
Comment Utility
ppittle's TryParse is the ideal option, it's what it is made for :)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:steeza
Comment Utility
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
Comment Utility
or "0" or whichever, you get the point :)
0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now