Cannot implement an interface member because it is not public

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Author

    Cannot implement an interface member because it is not public

    Well, I was trying out this code snippet from

    ..

    using System;
    using System.Collecti ons;

    public class SamplesArrayLis t {

    public class myReverserClass : IComparer {

    // Calls CaseInsensitive Comparer.Compar e with the parameters
    reversed.
    int IComparer.Compa re( Object x, Object y ) {
    return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
    }

    }

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

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

    int IComparer.Compa re( 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.
  • Stanimir Stoyanov

    #2
    Re: Cannot implement an interface member because it is not public

    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.Compa re( Object x, Object y ) // Default interface
    implementation

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

    --
    Stanimir Stoyanov
    메이저사이트 순위 먹튀검증이 완료된 토토사이트 커뮤니티 - 토토핫 입니다. 저희 토토핫에서는 베팅할때 필수로 점검해야 되는 안전 가이드를 제공하며 안전한 메이저 놀이터 목록과 메이저사이트 먹튀검증 꽁머니사이트를 추천드립니다.


    "Author" <gnewsgroup@gma il.comwrote in message
    news:240fd256-2bb4-4d89-bca9-4c6d9b4c3041@j2 2g2000hsf.googl egroups.com...
    Well, I was trying out this code snippet from

    .
    >
    using System;
    using System.Collecti ons;
    >
    public class SamplesArrayLis t {
    >
    public class myReverserClass : IComparer {
    >
    // Calls CaseInsensitive Comparer.Compar e with the parameters
    reversed.
    int IComparer.Compa re( Object x, Object y ) {
    return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
    }
    >
    }
    >
    public static void Main() {
    [snip]
    }
    }
    >
    Why do we have to explicitly qualify the Compare method as below
    >
    int IComparer.Compa re( 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.

    Comment

    • Author

      #3
      Re: Cannot implement an interface member because it is not public

      On Oct 30, 12:14 pm, "Stanimir Stoyanov"
      <stoya...@REMOV ETHIS.live.comw rote:
      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.Compa re( 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."

      ;-)

      Comment

      • Jesse Houwing

        #4
        Re: Cannot implement an interface member because it is not public

        Hello Author,
        Well, I was trying out this code snippet from
        Gain technical skills through documentation and training, earn certifications and connect with the community

        spx .
        >
        using System;
        using System.Collecti ons;
        public class SamplesArrayLis t {
        >
        public class myReverserClass : IComparer {
        >
        // Calls CaseInsensitive Comparer.Compar e with the parameters
        reversed.
        int IComparer.Compa re( Object x, Object y ) {
        return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
        }
        }
        >
        public static void Main() {
        [snip]
        }
        }
        Why do we have to explicitly qualify the Compare method as below
        >
        int IComparer.Compa re( 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.Copar e, 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


        Comment

        Working...