Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Null check on Convert.ToInt32

Posted on 2009-05-11
8
Medium Priority
?
917 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
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…

618 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