I am trying to implement a generic comparer (for a sort) for all objects that have a Text property... so two ASP.net textboxes can be compared, two labels or in this specific case two RadTreeNodes in a telerik RadTreeView (as long as they have a Text property). So I've put the following together to try and so this but get an error as follows:
I have the following code:
public class TextComparer<T> : IComparer
where T : IHasTextProperty
{
public int Compare(object a, object b)
{
T nodeA = (T)a;
T nodeB = (T)b;
return nodeA.Text.CompareTo(nodeB.Text);
}
}
public interface IHasTextProperty
{
string Text { get; set; }
}
Then plan on using it like so...
Array.Sort(nodes, new TextComparer<RadTreeNode>());
but get the following message :
Error 6613 The type 'Telerik.Web.UI.RadTreeNode' cannot be used as type parameter 'T' in the generic type or method 'TextComparer'. There is no implicit reference conversion from 'Telerik.Web.UI.RadTreeNode' to 'IHasTextProperty'
I'm sure this is a simple fix, but I'm just a little stumped as to how to fix it.