Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Interface Implementation Problem
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01470728
Message ID:
01470770
Views:
39
>I changed the scope of a method from public to protected, and now I get this:
>
>
>Chess.PieceBase' does not implement interface member 'Chess.IPiece.DetermineTargets()'. 'Chess.PieceBase.DetermineTargets()
>cannot implement an interface member because it is not public.	C:\Projects\Chess\Chess\Pieces\PieceBase.cs
>
>
>Why does a method defined in an interface need to be public?

From documentation:
"To implement an interface member, the corresponding member on the class must be public, non-static, and have the same name and signature as the interface member."

If a method is protected then, as Mike points out, there's no point including it in the interface definition; it won't be visible outside the derived classes and within those classes an interface is unneccessary

But can you explain exactly what behaviour you want in terms of scope?
Whilst an interface is public by default it *can* be made internal. So if you define the interface as internal and implement the method *explicitly* in the class then the method will only be accessible where the interface is in scope: e.g:
internal interface IPiece { void DetermineTargets();}

public class PieceBase : IPiece
    {
        void IPiece.DetermineTargets()
        {
        }
    }
DetermineTargets() can then be accessed from within the same assembly via:
((IPiece)new PieceBase()).DetermineTargets();
but will not be accessible elsewhere....
Previous
Reply
Map
View

Click here to load this message in the networking platform