?
Solved

Null check on Convert.ToInt32

Posted on 2009-05-11
8
Medium Priority
?
915 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Accepted Solution

by:
xbrady earned 2000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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