Casting generic parameters to int
-
Hi I have an array of integers, where each one is an index for an array of objects, and I'm trying to sort the array by the .name property of the objects. I've set up a class using the IComparer interface to do this.
List<int> SubjList = new List<int>(); SubjectIndexComparer<int> subjectIndexComparer = new SubjectIndexComparer<int>(); ... SubjList.Sort(subjectIndexComparer); public class SubjectIndexComparer<T> : IComparer<T> { ... public int Compare(T x, T y) { string textX = Singleton.Instance.subjList\[(int)x\].name; string textY = Singleton.Instance.subjList\[(int)y\].name; ... } }
However, this doesn't work because I can't cast x and y from type T to int. I only want the function to work if T can be casted to an integer. How can I implement this? I don't actually need the function to be generic, it would be fine if it just took integers, but the List.Sort() method required that form. Thanks in advance. Jack
-
Hi I have an array of integers, where each one is an index for an array of objects, and I'm trying to sort the array by the .name property of the objects. I've set up a class using the IComparer interface to do this.
List<int> SubjList = new List<int>(); SubjectIndexComparer<int> subjectIndexComparer = new SubjectIndexComparer<int>(); ... SubjList.Sort(subjectIndexComparer); public class SubjectIndexComparer<T> : IComparer<T> { ... public int Compare(T x, T y) { string textX = Singleton.Instance.subjList\[(int)x\].name; string textY = Singleton.Instance.subjList\[(int)y\].name; ... } }
However, this doesn't work because I can't cast x and y from type T to int. I only want the function to work if T can be casted to an integer. How can I implement this? I don't actually need the function to be generic, it would be fine if it just took integers, but the List.Sort() method required that form. Thanks in advance. Jack
Why don't you try with:
public class SubjectIndexComparer : IComparer
{
...
public int Compare(int x, int y)
{
...
}
}? Does that helps?
-
Why don't you try with:
public class SubjectIndexComparer : IComparer
{
...
public int Compare(int x, int y)
{
...
}
}? Does that helps?
I tried with:
public class SubjectIndexComparer : IComparer { ... public int Compare(int x, int y) { ... } }
but I got: 'UQTimetable.SubjectIndexComparer' does not implement interface member 'System.Collections.IComparer.Compare(object, object)' I tried with:
public class SubjectIndexComparer : IComparer { ... public int Compare(object x, object y) { ... } }
but I got: The best overloaded method match for 'System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)' has some invalid arguments Argument '1': cannot convert from 'UQTimetable.SubjectIndexComparer' to 'System.Collections.Generic.IComparer' Any more ideas?
Jack
-
I tried with:
public class SubjectIndexComparer : IComparer { ... public int Compare(int x, int y) { ... } }
but I got: 'UQTimetable.SubjectIndexComparer' does not implement interface member 'System.Collections.IComparer.Compare(object, object)' I tried with:
public class SubjectIndexComparer : IComparer { ... public int Compare(object x, object y) { ... } }
but I got: The best overloaded method match for 'System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)' has some invalid arguments Argument '1': cannot convert from 'UQTimetable.SubjectIndexComparer' to 'System.Collections.Generic.IComparer' Any more ideas?
Jack
Use this:
public class SubjectIndexComparer : IComparer<int>
{
public int Compare(int x, int y)
{
...
}
} -
Use this:
public class SubjectIndexComparer : IComparer<int>
{
public int Compare(int x, int y)
{
...
}
}:( that's what I meant... tnx for correcting me Daniel.
-
Use this:
public class SubjectIndexComparer : IComparer<int>
{
public int Compare(int x, int y)
{
...
}
}Worked a treat! Thanks
Jack