Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
public static class Cache
{
  public enum jobDetails { x }; 
}


Here is the variable defined in app.config:

C#
<add key="x" value=""/>


and the objectDetails is defined as in the following:

C#
public class objectDetails
{ 
   public int x { get; set; }
}


Here is the piece of code that I am having trouble with:

C#
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
Updated 21-Sep-12 8:23am
v2
Comments
[no name] 21-Sep-12 14:23pm    
String.IsNullOrWhiteSpace

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.
C#
objectDetails.x = 
   string.IsNullOrEmpty(Cache.jobDetails.x.ToString().Trim()) 
      ? 0 
      : Convert.ToInt16(jobDetails.GetValues(Cache.jobDetails.x.ToString().Trim())[0]);
 
Share this answer
 
v3
Comments
Member 8714829 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!
fjdiewornncalwe 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 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!!
VB
if (jobDetails.X.HasValue)
...{ assign to objectDetails}
 
Share this answer
 
v2
Comments
fjdiewornncalwe 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900