views:

61

answers:

2

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?

+4  A: 

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

BFree
+4  A: 

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.

Igor Zevaka
Hi, I have the following questions :1. By "subclass DataRowView", do u mean subclass of SelectionList?2. Do I have to override GetHashCode ?
Anish
No no, `DataRowView`. `GetHashCode` is only there as a quick hack. `CompareTo` should return an integer value indicating whether or not `this` is less than, equal or greater than `other` as far as sorting order goes.
Igor Zevaka
Can you answer my first question plz?
Anish
You need to subclass DataRowView, not SelectionList, as it is not a suitable class for SelectedList type constraints.
Igor Zevaka