Nullable<T> as a parameter
Posted
by ferch
on Stack Overflow
See other posts from Stack Overflow
or by ferch
Published on 2010-05-04T01:25:44Z
Indexed on
2010/05/04
1:38 UTC
Read the original article
Hit count: 247
I alredy have this:
public static object GetDBValue(object ObjectEvaluated)
{
if (ObjectEvaluated == null)
return DBNull.Value;
else
return ObjectEvaluated;
}
used like:
List<SqlParameter> Params = new List<SqlParameter>();
Params.Add(new SqlParameter("@EntityType", GetDBValue(EntityType)));
Now i wanted to keep the same interface but extend that to use it with nullable
public static object GetDBValue(int? ObjectEvaluated)
{
if (ObjectEvaluated.HasValue)
return ObjectEvaluated.Value;
else
return DBNull.Value;
}
public static object GetDBValue(DateTime? ObjectEvaluated)
{...}
but i want only 1 function GetDBValue for nullables. How do I do that and keep the call as is is? Is that possible at all?
I can make it work like:
public static object GetDBValue<T>(Nullable<T> ObjectEvaluated) where T : struct
{
if (ObjectEvaluated.HasValue)
return ObjectEvaluated.Value;
else
return DBNull.Value;
}
But the call changes to:
Params.Add(new SqlParameter("@EntityID ", GetDBValue<int>(EntityID)));
© Stack Overflow or respective owner