I have this method in an inherited DataGridView, but it won't compile
because the newly added special case is not allowed to cast 'string' to 'T'.
I know this cast will be valid whenever that code is reached. Is there a way
to do it, or must I make a separate non-generic method for the string case?
Eq.
public T[] GetSelectedColu mnValues<T>(int col)
{
T[] values = new T[SelectedRows.Co unt];
for (int i = 0; i < values.Length; i++)
{
if (values is string[]) // this way we can safely deal with DBNull as an
empty string
{
values[i] = (T) SelectedRows[i].Cells[col].Value.ToString ();
}
else // general case
{
values[i] = (T) SelectedRows[i].Cells[col].Value;
}
}
return values;
}
because the newly added special case is not allowed to cast 'string' to 'T'.
I know this cast will be valid whenever that code is reached. Is there a way
to do it, or must I make a separate non-generic method for the string case?
Eq.
public T[] GetSelectedColu mnValues<T>(int col)
{
T[] values = new T[SelectedRows.Co unt];
for (int i = 0; i < values.Length; i++)
{
if (values is string[]) // this way we can safely deal with DBNull as an
empty string
{
values[i] = (T) SelectedRows[i].Cells[col].Value.ToString ();
}
else // general case
{
values[i] = (T) SelectedRows[i].Cells[col].Value;
}
}
return values;
}
Comment