tags:

views:

91

answers:

1

I am using the ObservableSortedDictionary from Dr. WPF.

The constructor looks like this:

public ObservableSortedDictionary(IComparer<DictionaryEntry> comparer)

I am really struggling to create an implementation that satisfies the constructor and works.

My current code (that won't compile) is:

public class TimeCreatedComparer<T> : IComparer<T> 
{
    public int Compare(T x, T y)
    {
        var myclass1 = (IMyClass)((DictionaryEntry)x).Value;
        var myclass2 = (IMyClass)((DictionaryEntry)y).Value;
        return myclass1.TimeCreated.CompareTo(myclass2.TimeCreated);
    }
}

It says I can't cast from T to DictionaryEntry.

If I cast directly to IMyClass, it compiles, but I get a runtime error saying I can't cast from DictionaryEntry to IMyClass. At runtime, x and y are instances of DictionaryEntry, which each have the correct IMyClass as their Value.

+3  A: 
public class TimeCreatedComparer : IComparer<DictionaryEntry> 
{
    public int Compare(DictionaryEntry x, DictionaryEntry y)
    {
        var myclass1 = (IMyClass)x.Value;
        var myclass2 = (IMyClass)y.Value;
        return myclass1.TimeCreated.CompareTo(myclass2.TimeCreated);
    }
}

Does this do what's needed?

yodaj007
Arggggggggggg. So simple.
Phil Sandler