Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Got the following code to assign a value which is defined in the app.config and Cache, this value is retrieved in cache as "public enum class" such as in the following:
 
public static class Cache
{
  public enum jobDetails { x }; 
}
 
Here is the variable defined in app.config:
 
<add key="x" value=""/>
 
and the objectDetails is defined as in the following:
 
public class objectDetails
{ 
   public int x { get; set; }
}
 
Here is the piece of code that I am having trouble with:
 
 objectDetails.x = Convert.ToInt16(jobDetails.GetValues(Cache.jobDetails.x.ToString().Trim())[0]);
 
My question is here is if "x" is defined as null value ("") in the app.config file then the above code would fail (due to string format error), so how can I check the value upfront if it is a null value ("") then assign a 0 to it before assigning the value to objectDetails.x (and do that elegantly).
Posted 21-Sep-12 8:20am
Edited 21-Sep-12 8:23am
v2
Comments
Wes Aday at 21-Sep-12 14:23pm
   
String.IsNullOrWhiteSpace
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The error you are receiving is because Convert.ToInt16 will throw an exception if the argument cannot be parsed into a 16bit integer, so you have to ensure that if you make a call to Convert.ToInt16 the argument you supply it is valid.
 
In your case the problem in Cache.jobDetails.X.ToString() being empty or null, so string.IsNullOrEmpty(Cache.jobDetails.x.ToString()) is what you need to check.
 
eg.
objectDetails.x = 
   string.IsNullOrEmpty(Cache.jobDetails.x.ToString().Trim()) 
      ? 0 
      : Convert.ToInt16(jobDetails.GetValues(Cache.jobDetails.x.ToString().Trim())[0]);
  Permalink  
v3
Comments
Member 8714829 at 21-Sep-12 14:47pm
   
This looks like something I can use and re use through out my code to update all properties like this one. One comment to mention, I made it work by changing your solution as in the following:
objectDetails.x =
string.IsNullOrEmpty(Convert.ToInt16(jobDetails.GetValues(Cache.jobDetails.x.ToString().Trim())[0]) ? 0 : Convert.ToInt16(jobDetails.GetValues(Cache.jobDetails.x.ToString().Trim())[0]);
 
thank you!
Marcus Kramer at 21-Sep-12 14:51pm
   
Your change won't work. The change you made effectively makes the statement act EXACTLY as your original problem statement. The issue is that Convert.ToInt16 will throw the exception if Cache.JobDetails.x.ToString() is empty or null, so doing a string.IsNullOrEmpty on the Convert statement will still result in an exception. Use the code the way I gave it to you because yours won't work.
Member 8714829 at 24-Sep-12 13:52pm
   
Thank you for the note, I've detected this issue last week during my testing and made the correction, again many thanks for your follow up!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

if (jobDetails.X.HasValue)
...{ assign to objectDetails}
  Permalink  
v2
Comments
Marcus Kramer at 21-Sep-12 14:35pm
   
My vote of 1: It's not a nullable type.

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

  Print Answers RSS
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 200
2 Rob Philpott 100
3 jlopez788 99
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,589
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100