How can I use generic here
I am trying to use generic for the first time and trying to typecast my result returned from database to programmer defined data type. How can I do this.
dsb.ExecuteQuery("DELETE FROM CurrencyMaster WHERE CurrencyMasterId="
+ returnValueFromGrid<int>(getSelectedRowIndex(), "CurrencyMasterId"));
private T returnValueFromGrid<T>(int RowNo, string ColName)
{
return Convert.ChangeType(dgvCurrencyMaster.R开发者_如何学Cows[RowNo].Cells[ColName].Value, T);
}
You're trying to use T
as a value - you want the type T represents, and then you'll need a cast as well:
object value = dgvCurrencyMaster.Rows[RowNo].Cells[ColName].Value;
return (T) Convert.ChangeType(value, typeof(T));
精彩评论