Click here to Skip to main content
12,302,514 members (65,450 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

25.7K views
4 bookmarked
Posted

Converting a nullable object to an integer

, 16 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Type conversion
I decided to write this tip/trick because my question[^] on the CodeProject's
questions[^] forum lead to quite some different thoughts.

How do we convert objects to integers?
  1  int convertedInteger;
  2  object someValue; // Can be anything
  3  
  4  // The conversion
  5  Nullable<int> converted = someValue as Nullable<int>
  6  // Or shorter
  7  int? converted = someValue as int?;
  8  
  9  convertedInteger = (converted.HasValue) ? converted.Value : -1;

So for example, if you execute a Microsoft SQL Stored Procedure of which you are sure it returns an integer (or of course a null value), the best way is to use the returned value:

  1  int storedProcedureResult = -1; // Default value is -1
  2  
  3  using (SqlCommand cmdMatch = new SqlCommand("spName", Connection))
  4  {
  5    cmdMatch.CommandType = System.Data.CommandType.StoredProcedure;
  6    int? result = cmdMatch.ExecuteScalar() as int?;
  7    storedProcedureResult = (result.HasValue) ? result.Value : -1;
  8  }

Some improvements to the code above (thanks to AspDotNetDev[^] who added an alternate):
  1  int storedProcedureResult = -1; // Default value = -1
  2  
  3  using (SqlCommand cmdMatch = new SqlCommand("spName", Connection))
  4  {
  5    cmdMatch.CommandType = System.Data.CommandType.StoredProcedure;
  6    var converted = cmdMatch.ExecuteScalar() as int?;
  7    int storedProcedureResult = converted ?? -1;
  8  }

It may have been obvious to all you guys, but I struggled having to convert the returned object first to a string in order to use int.Parse() and int.TryParse():
  1  
  2  // This block is marked red because this is NOT the correct way
  3  int storedProcedureResult = -1; // Default value is -1
  4  
  5  using (SqlCommand cmdMatch = new SqlCommand("spName", Connection))
  6  {
  7  
  8    cmdMatch.CommandType = System.Data.CommandType.StoredProcedure;
  9    object result = cmdMatch.ExecuteScalar();
 10    if (result != null)
 11    {
 12      int.TryParse(result.ToString(), out storedProcedureResult);
 13    }
 14  }

Now we all know the neat way. Wink | ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Eduard Keilholz
Software Developer (Senior) http://www.today-it.nl
Netherlands Netherlands
In 1998 I started as webdesigner programming websites in Perl and later PHP. After two years wrote most of the websites in ASP and from then on lost the feeling with a linux/unix platform.

Since 2001 interested in Windows applications and now writing software using mostly C# for about 7 years now.

You may also be interested in...

Comments and Discussions

 
GeneralWell done! Proper handling of the null is utmost important i... Pin
DrABELL12-May-11 7:09
memberDrABELL12-May-11 7:09 
GeneralReason for my vote of 5 Excellent, thanks! Pin
edjeit18-Mar-11 5:35
memberedjeit18-Mar-11 5:35 
QuestionRed Code Block? Pin
AspDotNetDev18-Mar-11 7:53
mvpAspDotNetDev18-Mar-11 7:53 
AnswerRe: Red Code Block? Pin
Eduard Keilholz18-Mar-11 9:08
memberEduard Keilholz18-Mar-11 9:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 16 May 2011
Article Copyright 2011 by Eduard Keilholz
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid