Cannot implement an interface member because it is not public

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.
 
S

Stanimir Stoyanov

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
 
A

Author

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."

;-)
 
J

Jesse Houwing

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.
 

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

Top