patterncsharpMinor
Better way of parsing string to boolean
Viewed 0 times
booleanwaybetterparsingstring
Problem
At the moment I am using this to check a boolean property in sharepoint 2007 using C# and .Net Framework 3.5
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"
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
-
Validate that the object exists before you call
-
Use
-
If the hashed value that corresponds to the "Boolean Property" key is really a
-
Eliminate magic strings from your value accessor. This requires removing the inline
```
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 pro
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 a NullReferenceException if the key "Boolean Property" is missing or the hashed value that corresponds to the "Boolean Property" key is null. 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 of bool.Parse. If the ToString() value of the hashed value corresponding to the key "Boolean Property" is anything other than "true" or "false" (case-insensitive), a FormatException 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. Calling ToString() 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 pro
Code Snippets
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
}
}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
}
}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"
}
}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
}
}
}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
}
}
}Context
StackExchange Code Review Q#16349, answer score: 8
Revisions (0)
No revisions yet.