Try our conversational search powered by Generative AI!

Value check return emtpy even if the value exists


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?

Apr 22, 2008 11:11

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;
Apr 22, 2008 13:16

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

bool showNewsFA = StartPage["NewsFocusArea"] as bool? ?? false;
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.