I have a Visual Studio 2008 C# 2.0 CF project where I am implementing a generic interface, IComparison
. The IComparison.Compare
method may be called on to do any type of comparison that is valid to for the objects specified, so I don't want to put a type constraint on it.
public interface IComparison<EXPECTED_PARAM>
{
Result Compare<RETURNED_PARAM>(RETURNED_PARAM returned);
}
The implementation, however, can be more specific. In this case, I'd like to say that the parameter given to GreaterThan.Compare
can be compared to the EXPECTED_PARAM
given in the constructor via System.IComparable
.
public class GreaterThan<EXPECTED_PARAM> : IComparison<EXPECTED_PARAM>
{
private EXPECTED_PARAM expected_;
public GreaterThan(EXPECTED_PARAM expected)
{
expected_ = expected;
}
public Result Compare<RETURNED_PARAM>(RETURNED_PARAM returned)
where RETURNED_PARAM : IComparable< EXPECTED_PARAM >
{
return ((returned == null && expected_ == null) ||
(returned != null && returned.CompareTo( expected_ ) > 0)) ?
Result.Fail : Result.Pass;
}
}
Unfortunately, this gives me the error:
error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
What do I need to do to get the ability to perform arbitrary comparisons of EXPECTED_PARAM
objects with RETURNED_PARAM
objects?
Thanks, PaulH
How about this?
public interface IComparison<EXPECTED_PARAM, RETURNED_PARAM>
{
Result Compare(RETURNED_PARAM returned);
}
public class GreaterThan<EXPECTED_PARAM, RETURNED_PARAM> : IComparison<EXPECTED_PARAM, RETURNED_PARAM> where RETURNED_PARAM : IComparable<EXPECTED_PARAM>
{
private EXPECTED_PARAM expected_;
public GreaterThan(EXPECTED_PARAM expected)
{ expected_ = expected; }
public Result Compare(RETURNED_PARAM returned)
{
return ((returned == null && expected_ == null) ||
(returned != null && returned.CompareTo( expected_ ) > 0)) ?
Result.Fail : Result.Pass;
}
}