C
C#•12mo ago
LeviCoding

IComparer problem

I get an error with the I comparer implementation.
No description
No description
19 Replies
LeviCoding
LeviCodingOP•12mo ago
We have to use the IComparer like in this example
LeviCoding
LeviCodingOP•12mo ago
No description
No description
No description
No description
LeviCoding
LeviCodingOP•12mo ago
this is my code
LeviCoding
LeviCodingOP•12mo ago
this is the error
No description
alex
alex•12mo ago
currently RatingComparer implements Domein.IComparer<Domein.Movie>, but instead it should implement System.Collections.Generic.IComparer<Domein.Movie
LeviCoding
LeviCodingOP•12mo ago
And how can I do this?
alex
alex•12mo ago
remove Domein.IComparer<T> completely, the linq functions expect a System.Collections.Generic.IComparer<T>
LeviCoding
LeviCodingOP•12mo ago
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WC_Movie.Domein;

namespace WC_Movie.Domein
{
public class RatingComparer : IComparer <Movie>
{
public int Compare(Movie m1, Movie m2)
{
int firstComp = m1.Rating.CompareTo(m2.Rating);
return firstComp;
}
}
}
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WC_Movie.Domein;

namespace WC_Movie.Domein
{
public class RatingComparer : IComparer <Movie>
{
public int Compare(Movie m1, Movie m2)
{
int firstComp = m1.Rating.CompareTo(m2.Rating);
return firstComp;
}
}
}
I cant see da domein.IComparer<T> i this code
alex
alex•12mo ago
its implicitly using IComparer <Movie> from Domein
LeviCoding
LeviCodingOP•12mo ago
So I have to remove a using statement?
alex
alex•12mo ago
if you want to use the other one, you need to specify it with the full path, since your own namespace is implicitly preferred over the other one
LeviCoding
LeviCodingOP•12mo ago
a using domain?
alex
alex•12mo ago
that would work probably but you should remove your definition of Domein.IComparer<T> completely, you dont need it for anything
LeviCoding
LeviCodingOP•12mo ago
But where in my code is the definition xp in my RatingComparer class or in the interface
alex
alex•12mo ago
movies.Sort method expects a System.Collections.Generic.IComparer<T>, your comparer class must implement that it doesnt accept a Domein.IComparer<T>, because the base class library doesnt know about your types
LeviCoding
LeviCodingOP•12mo ago
No description
LeviCoding
LeviCodingOP•12mo ago
ok it works, but why having "using System.Collections.Generic;" if we still need to type it full
alex
alex•12mo ago
because the name conflicts with Domein.IComparer<T>
LeviCoding
LeviCodingOP•12mo ago
So I have to IComparers. Hmmm idk thank your for your help alex 😄 I'll take some time to process it xp
Want results from more Discord servers?
Add your server