IComparable Question

R

raffelm

In the first class below, I get an error that
AnotherClientBaseDataRecord does not implement
IComparable.CompareTo(object)...yet the second class below is
implemented identicially and it does get the compile error. I'd love
help understanding this :=) ....

Why does this not compile
public class AnotherClientBaseDataRecord : IComparable
{
public AnotherClientBaseDataRecord() {}
public int CompareTo(AnotherClientBaseDataRecord otherRecord) {
return 0;}
}

public class BuildFileRecord : IComparable
{
public BuildFileRecord () {}
public int CompareTo(BuildFileRecord otherRecord) { return 0;}
}
 
J

Joe Mayo [C# MVP]

raffelm said:
In the first class below, I get an error that
AnotherClientBaseDataRecord does not implement
IComparable.CompareTo(object)...yet the second class below is
implemented identicially and it does get the compile error. I'd love
help understanding this :=) ....

Why does this not compile
public class AnotherClientBaseDataRecord : IComparable
{
public AnotherClientBaseDataRecord() {}
public int CompareTo(AnotherClientBaseDataRecord otherRecord) {
return 0;}
}

public class BuildFileRecord : IComparable
{
public BuildFileRecord () {}
public int CompareTo(BuildFileRecord otherRecord) { return 0;}
}

Hi raffelm,

You need to make the signature of the CompareTo method match what is defined
in IComparable:

public class AnotherClientBaseDataRecord : IComparable
{
public AnotherClientBaseDataRecord() {}
public int CompareTo(object otherRecord)
{
return 0;}
}

public class BuildFileRecord : IComparable
{
public BuildFileRecord () {}
public int CompareTo(object otherRecord) { return 0;}
}

BTW, you should have received compile errors for both classes. To prove
that you will receive an error for BuildFileRecord, fix
AnotherClientBaseRecord first and recompile.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top