PC Review


Reply
Thread Tools Rate Thread

Cannot implement an interface member because it is not public

 
 
Author
Guest
Posts: n/a
 
      30th Oct 2008
Well, I was trying out this code snippet from
http://msdn.microsoft.com/en-us/libr...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.
 
Reply With Quote
 
 
 
 
Stanimir Stoyanov
Guest
Posts: n/a
 
      30th Oct 2008
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

--
Stanimir Stoyanov
http://stoyanoff.info

"Author" <(E-Mail Removed)> wrote in message
news:240fd256-2bb4-4d89-bca9-(E-Mail Removed)...
> Well, I was trying out this code snippet from
> http://msdn.microsoft.com/en-us/libr...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.


 
Reply With Quote
 
Author
Guest
Posts: n/a
 
      30th Oct 2008
On Oct 30, 12:14*pm, "Stanimir Stoyanov"
<stoya...@REMOVETHIS.live.com> wrote:
> 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
>
> --
> Stanimir Stoyanovhttp://stoyanoff.info
>


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

;-)

 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      30th Oct 2008
Hello Author,

> Well, I was trying out this code snippet from
> http://msdn.microsoft.com/en-us/libr...ns.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.

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Error: InterfaceDictionary ... does not implement interface member ... Rich P Microsoft C# .NET 0 1st Jul 2010 09:08 PM
Error: InterfaceDictionary ... does not implement interface member ... Rich P Microsoft C# .NET 0 1st Jul 2010 05:46 PM
abstract class 'does not implement interface member ...' Ben Voigt [C++ MVP] Microsoft C# .NET 52 25th Jun 2007 02:13 PM
Interface vs Public class member ?? =?Utf-8?B?Y2FsZGVyYXJh?= Microsoft Dot NET 3 26th Apr 2007 12:08 PM
Base class member implement interface member Armin Zingler Microsoft VB .NET 0 5th Feb 2006 03:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:50 AM.