November Happy Hour will be moved to Thursday December 5th.

Value check return emtpy even if the value exists

Vote:
 

I use this code to check if a value exists and has a value

string showNewsFA = StartPage["NewsFocusArea"] as string ?? string.Empty;

The problem I'm having with some of these is that they return an empty string even though the property exists and has a value. How can that be?

In this case I'm checking for a property of type checkbox if that has anything to do with the problem?

#19630
Apr 22, 2008 11:11
Vote:
 

You're trying to convert the property value into a string, if that fails (which it will to for a checkbox property), an empty string is returned. Try something similar to this:

bool showNewsFA = StartPage["NewsFocusArea"] != null 
? (bool)StartPage["NewsFocusArea"] : false;
#19633
Apr 22, 2008 13:16
Vote:
 

I tried some different ways and I got this to work also

bool showNewsFA = StartPage["NewsFocusArea"] as bool? ?? false;
#19636
Apr 22, 2008 14:14
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.