I have this code that works ok with a database with no NULL values
public T GetField<T>(SqlDataReader dr, string fieldName)
{
return (T)dr.GetValue(dr.GetOrdinal(fieldName));
}
Then I want to control the DBNull values, because they already exist in some tables and I upgraded the function like this:
public T GetField<T>(SqlDataReader dr, string fieldName) where T : new()
{
var value = dr.GetValue(dr.GetOrdinal(fieldName));
return value is DBNull ? new T() : (T)value;
}
But now, I cannot ask for GetField<string>
because string has no constructor with 0 parameters.
I have tried to create another function like the Abstract one, with no restrictions and only with string type like this
public string GetField<string>(SqlDataReader dr, string fieldName)
{
var value = dr.GetValue(dr.GetOrdinal(fieldName));
return value is DBNull ? "" : value.ToString();
}
but the answer is that they are ambiguous definitions, because T also includes string, even with the restriction applied.
I could create independent functions for each datatype, but I rather prefer the abstract solution because it is way much cleaner.
Did I miss something?
Thanks!
Here's one approach:
This has the added benefit of working with nullable types. Then you'd call that as:
Which is deliberately contrived in this case -- Jon's version with a default suits you better if usually you want something other than
null
returned.