Cannot implement an interface member because it is not public

  • Thread starter Thread starter Author
  • Start date Start date
A

Author

Well, I was trying out this code snippet from
http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx
..

using System;
using System.Collections;

public class SamplesArrayList {

public class myReverserClass : IComparer {

// Calls CaseInsensitiveComparer.Compare with the parameters
reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}

}

public static void Main() {
[snip]
}
}

Why do we have to explicitly qualify the Compare method as below

int IComparer.Compare( Object x, Object y )

when we implement the Compare method of the IComparer interface?

If I remove qualification, and simply do

int Compare( Object x, Object y )

I get a compile time error which says:

"Cannot implement an interface member because it is not public."

I don't understand this. Please kindly advise. TIA.
 
Since myReverserClass implements IComparer's methods, they must be with
public accessibility so that outer objects can interact with the interface
members. The following two examples will work:

int IComparer.Compare( Object x, Object y ) // Default interface
implementation

public int Compare( Object x, Object y ) // Notice the 'public' access
modifer
 
Since myReverserClass implements IComparer's methods, they must be with
public accessibility so that outer objects can interact with the interface
members. The following two examples will work:

int IComparer.Compare( Object x, Object y ) // Default interface
implementation

public int Compare( Object x, Object y ) // Notice the 'public' access
modifer

Yes, that's correct. I didn't notice that the public qualifier is
missing. Maybe the compiler error message can be more friendly like

"By any chance could you try making this Compare method explicitly
public? Simply add "public" to the front and see what happens."

;-)
 
Hello Author,
Well, I was trying out this code snippet from
http://msdn.microsoft.com/en-us/library/system.collections.icomparer.a
spx .

using System;
using System.Collections;
public class SamplesArrayList {

public class myReverserClass : IComparer {

// Calls CaseInsensitiveComparer.Compare with the parameters
reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}

public static void Main() {
[snip]
}
}
Why do we have to explicitly qualify the Compare method as below

int IComparer.Compare( Object x, Object y )

when we implement the Compare method of the IComparer interface?

If I remove qualification, and simply do

int Compare( Object x, Object y )

I get a compile time error which says:

"Cannot implement an interface member because it is not public."

I don't understand this. Please kindly advise. TIA.


When you use IComparer.Copare, the compiler will egnerate a public method
for you. But if you don't specity the explicit interface name, it will default
to private.

To solve your issue, simply change the signiatyure to public inr Compare
and your problems will go away.
 
Back
Top