Problem
At the moment I am using this to check a boolean property in sharepoint 2007 using C# and .Net Framework 3.5
if (bool.Parse(listItem.Properties["Boolean Property"].ToString()) == false)
is there any better way of doing it ?
Edit
when my code try to convert property into string it gives me exception even when am validating if property is empty or not.
This Worked For me
using method “Contains Key” instead of “Properties.Containsfield”
object abc = "bool property";
foreach (SPListItem item in list.Items)
{
if (item.Properties.ContainsKey(abc)
Solution
I am making some assumptions about your code, specifically that listItem
is of type SPListItem
and that listItem.Properties
is a HashTable
. This is mainly to provide context for my explanation, but even if I am mistaken, most of my comments will still hold. For the sake of context, let’s assume your actual method looks like:
void HandleBooleanListItemProperty( SPListItem listItem ){
if (bool.Parse(listItem.Properties["Boolean Property"].ToString()) == false){
// do something if the Boolean ListItem property is false
}
else
{
// do something if the Boolean ListItem property is true
}
}
-
Validate that the object exists before you call
ToString()
. The way your method is currently written, you will get aNullReferenceException
if the key “Boolean Property” is missing or the hashed value that corresponds to the “Boolean Property” key isnull
. This refactoring will make the method look like:void HandleBooleanListItemProperty2( SPListItem listItem ){ object booleanPropertyValue = listItem.Properties["Boolean Property"]; if ( ReferenceEquals( booleanPropertyValue, null ) ) { // do something if the Boolean ListItem property is missing } else if (bool.Parse(booleanPropertyValue.ToString()) == false){ // do something if the Boolean ListItem property is false } else { // do something if the Boolean ListItem property is true } }
-
Use
bool.TryParse
instead ofbool.Parse
. If theToString()
value of the hashed value corresponding to the key “Boolean Property” is anything other than “true” or “false” (case-insensitive), aFormatException
will be thrown. This refactoring will make the method look like:void HandleBooleanListItemProperty3( SPListItem listItem ){ object booleanPropertyValue = listItem.Properties["Boolean Property"]; bool parsedBooleanPropertyValue; if ( ReferenceEquals( booleanPropertyValue, null ) ) { // do something if the Boolean ListItem property is missing } else if (bool.TryParse(booleanPropertyValue.ToString(), out parsedBooleanPropertyValue) ) { if(parsedBooleanPropertyValue == false){ // do something if the Boolean ListItem property is false } else { // do something if the Boolean ListItem property is true } } else { // do something if the Boolean ListItem property's ToString() value was something other than "true" or "false" } }
-
If the hashed value that corresponds to the “Boolean Property” key is really a
bool
, then cast it before comparing. CallingToString()
just throws away .NET’s type-safety. This refactoring will make the method look like:void HandleBooleanListItemProperty4( SPListItem listItem ){ object booleanPropertyValue = listItem.Properties["Boolean Property"]; if (booleanPropertyValue is bool) { bool castBooleanPropertyValue = (bool) booleanPropertyValue; if( castBooleanPropertyValue == false ){ // do something if the Boolean ListItem property is false } else { // do something if the Boolean ListItem property is true } } else { // do something if the Boolean ListItem property is not a bool if ( ReferenceEquals( booleanPropertyValue, null ) ) { // do something if the Boolean ListItem property is missing } else { // do something if the Boolean ListItem property is something other than a bool } } }
-
Eliminate magic strings from your value accessor. This requires removing the inline
"Boolean Property"
and declaring with that value, and then using the constant in order to access the value in every usage. This decreases the possibility that a typos will cause bugs. An article on magic strings is available at: http://codebetter.com/matthewpodwysocki/2009/03/19/functional-net-lose-the-magic-strings/ . The resulting refactoring would look like:const string BooleanPropertyKey = "Boolean Property"; void HandleBooleanListItemProperty5( SPListItem listItem ){ object booleanPropertyValue = listItem.Properties[BooleanPropertyKey]; if (booleanPropertyValue is bool) { bool castBooleanPropertyValue = (bool) booleanPropertyValue; if( castBooleanPropertyValue == false ){ // do something if the Boolean ListItem property is false } else { // do something if the Boolean ListItem property is true } } else { // do something if the Boolean ListItem property is not a bool if ( ReferenceEquals( booleanPropertyValue, null ) ) { // do something if the Boolean ListItem property is missing } else { // do something if the Boolean ListItem property is something other than a bool } } }
-
Use the negation operator (
!
) instead of comparing to false. This is much more readable and will save you time in the long run. This final refactoring looks like:const string BooleanPropertyKey = "Boolean Property"; void HandleBooleanListItemProperty6( SPListItem listItem ){ object booleanPropertyValue = listItem.Properties[BooleanPropertyKey]; if (booleanPropertyValue is bool) { bool castBooleanPropertyValue = (bool) booleanPropertyValue; if( !castBooleanPropertyValue ){ // do something if the Boolean ListItem property is false } else { // do something if the Boolean ListItem property is true } } else { // do something if the Boolean ListItem property is not a bool if ( ReferenceEquals( booleanPropertyValue, null ) ) { // do something if the Boolean ListItem property is missing } else { // do something if the Boolean ListItem property is something other than a bool } } }
The .Parse
methods will throw exceptions if the value is null, in a bad format, etc.
I suggest always using the .TryParse
methods instead:
bool val;
if(!bool.TryParse(listItem.Properties["Boolean Property"].ToString(), out foo)) {
val = false;
}
Or perhaps one of the Convert.ToBoolean()
methods.