Click here to Skip to main content
13,090,930 members (42,550 online)
Rate this:
Please Sign up or sign in to vote.
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:

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
Updated 21-Sep-12 8:23am
Wes Aday 21-Sep-12 14:23pm
Rate this: bad
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.

objectDetails.x = 
      ? 0 
      : Convert.ToInt16(jobDetails.GetValues(Cache.jobDetails.x.ToString().Trim())[0]);
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!
Marcus Kramer 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!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

if (jobDetails.X.HasValue)
...{ assign to objectDetails}
Marcus Kramer 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2017
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