I would like to create a helper class to do some common task.
For example, I am retrieving some results from Database and then assigning the values to variables. Bu
Well what you are trying to acchieve is to avoid the NullReferenceException i guess.
You could achieve this by writing a generic method like this
public static TValue GetValueSafe(TObject obj, Func accessor)
{
if(obj== null)
return default(TValue);
return accessor(obj);
}
Then use it like this:
string strValue = Helpers.GetValueSafe(dr[colName], o => o.toString());
This would either return the value of toString, or if dr[colName] == null returns default(string) which is null.
You could exand this by adding a defaultParameter to define a value on "failure".
However i would'nt recommend using this.