I have a class in my program called SortableObservableCollection<T> that inherits from ObservableCollection<T>. It has a function called Sort:
public void Sort<TKey>( Func<T, TKey> keySelector, int skip = 0 )
{
// . . .
}
I want to declare a property called Key1 of type Func<T, TKey>. I have tried:
public Func<T, TKey> Key1<T, TKey> { get; set; }
But I am getting a syntax error on the left curly brace. The error indicates that the compiler is expecting a left parentheses. I’ve tried making it a field declaration:
public Func<T, TKey> Key1<T, TKey>;
But then the compiler gives me the same message on the semi-colon.
What’s the right syntax for declaring this property?
While everyone’s answers will work, they weren’t exactly where I was trying to get with this. Originally, I had a method of the class called
Sort:This worked well, but then I found a bug in my UI that was related to calling the Sort method more than once. The sort itself worked, that wasn’t the problem. The bug is convoluted and difficult to explain, and I’d rather not go into it. But the easiest solution was to make sure that the collection was sorted only once, on the correct key. The thing is that the keys differed depending on the particular drop down that the collection was bound to. So I needed a way to encapsulate the sort criteria in the collection so it would do the proper sort the first and only time.
As I already had the
Sortmethod mentioned above, I figured the easiest thing to do was to put the keySelector parameter to it into a property of the class. It turns out that the language doesn’t let you do that, and now I know and understand why.What I ended up doing to get this to work was replace the
Funcproperty with a reference to an object that implementsIComparer<T>. I have no trouble declaring this property and it’s easy to declare a class that implements it for each type passed as theTparameter in a collection declaration. I just initialize the property after allocating the collection object and I rewrote theSortmethod so it looks like this:RowsToSkipis anintproperty of the class which specifies how many rows at the start of the collection are to be skipped. This lets me add an entry like “Pick a choice” at the start of the list and not have the sort move it.