1
I’m trying to use the code below to convert but it gives error everywhere that has the T
and where
. What could be?
// function that set the given object from the given data row
public static void SetItemFromRow(T item, DataRow row)
where T : new()
{
// go through each column
foreach (DataColumn c in row.Table.Columns)
{
// find the property for the column
PropertyInfo p = item.GetType().GetProperty(c.ColumnName);
// if exists, set the value
if (p != null && row[c] != DBNull.Value)
{
p.SetValue(item, row[c], null);
}
}
}
// function that creates an object from the given data row
public static T CreateItemFromRow(DataRow row)
where T : new()
{
// create a new object
T item = new T();
// set the item
SetItemFromRow(item, row);
// return
return item;
}
// function that creates a list of an object from the given data table
public static List CreateListFromTable(DataTable tbl)
where T : new()
{
// define return list
List lst = new List();
// go through each row
foreach (DataRow r in tbl.Rows)
{
// add to the list
lst.Add(CreateItemFromRow(r));
}
// return the list
return lst;
}
I may be wrong, but T is what? The compiler doesn’t know what T is, I think that’s why it makes the mistake. Different from a generic list with generic type T. If I’m wrong please correct me.
– pnet