kamagra how much to take

Getting Safe String Values

I know there are better ways of getting SharePoint field values then indexing the SPListItem for a particular field and using archaic string manipulation to make things dance. But sometimes when you are retrofitting a legacy application it can be intrusive to the holistic application and cause all sorts of issues in other portions.

While doing a code review today at a client this came up. We were discussing when using the SPListItem indexing approach how to best get safe string values. The extension method in use that we were discussing today was:

  1. public static string ToSafeString<T>(this T obj)
  2.         {
  3.             if (typeof(T).IsValueType)
  4.             {
  5.                 return obj.ToString();
  6.             }
  7.             return Equals(obj, null) ? string.Empty : obj.ToString();
  8.         }

Now I don’t think that this is too bad, but doesn’t Convert.ToString() already handle null values? I guess I can see the use since it will show on every derived class depending on the implementation of the extension and might look more natural from a language perspective. Furthermore, I find the value type guard clause unnecessary, and could be simply truncated as written as a helper method i.e.:

  1. return value == null ? string.Empty, obj.ToString();
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>