I have this code:
public class SelectionList<T> : ObservableCollection<SelectionItem<T>> where T : IComparable<T>
{
// Code
}
public class SelectionItem<T> : INotifyPropertyChanged
{
// Code
}
I need to create a property which is of the type SelectionList
as follows:
public SelectionList<string> Sports { get; set; }
But when I replace string with DataRowView, as
public SelectionList<DataRowView> Sports { get; set; }`
I am getting an error. Why doesn't this work?
Your problem is that string
implements IComparable<string>
and DataRowView
doesn't.
SelectionList<T>
has a constraint that T
must implement IComparable<T>
, hence the error.
public class SelectionList<T> : ObservableCollection<SelectionItem<T>> where T : IComparable<T>
{
// Code
}
One solution would be to subclass DataRowView and implement IComparable
:
public class MyDataRowView : DataRowView, IComparable<DataRowView>{
int CompareTo(DataRowView other) {
//quick and dirty comparison, assume that GetHashCode is properly implemented
return this.GetHashCode() - (other ? other.GetHashCode() : 0);
}
}
Then SelectionList<MyDataRowView>
should compile fine.
You have a constraint on your class where T : IComparable<T>
. DataRowView does not implement IComparable<DataRowView>
and therefore cannot be used in this case.
See here for more information about Generic Constraints: http://msdn.microsoft.com/en-us/library/d5x73970.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.