How can I use generic here

I am trying to use generic for the first time and trying to match my result from the database to the type of data defined by the programmer. 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.Rows[RowNo].Cells[ColName].Value, T);
}
+3
source share
1 answer

You are trying to use Tas a value - you want a type T to represent, and then you will also need a cast:

object value = dgvCurrencyMaster.Rows[RowNo].Cells[ColName].Value;
return (T) Convert.ChangeType(value, typeof(T));
+3
source

Source: https://habr.com/ru/post/1742423/


All Articles