i am trying to write a function that will make DataRow[column] return nullable typed data. for example: if i have a property
int? iValue { get; set; }
and the DataRow["ID"] will have the int ID in the Database i want to make a wrapper function that will check if the value of the DataRow["ID"] is DBnull and set the iValue to null. and so on for the rest of the datatypes. i wrote the following function
public static void CDB<T>(ref T tData, object objDBDataItem)
{
if (objDBDataItem.GetType().FullName == "System.DBNull")
tData = default(T);
tData = (T)objDBDataItem;
}
to call the function i do the following
CDB(ref iValue , DataRow["ID"]);
this should initialize the value of iValue to the integer value of DataRow["ID"] or null if the value in the DB is null. the problem is that this code dont work because i am not allowed to pass proprities as reference. but the idea is that i dont want to call the function as
iValue = CDB<int>(DataRow["ID"]);
so far i was able to make the function work if i change it to be called in the format of
iValue = CDB(iValue, DataRow["ID"]);
but i dont like the idea that i need to repeat iValue (one time to get the Type and one time so that i assign the return value to it.
any suggesions?