Events when implementing Interfaces
Hi - Hobby coder here. Trying to expand my coding. My query is around using Events in Interfaces. Let's say I have an interface that includes:
When I do "Implement reminaing memebers explicity" (I often add to my inerface as I go) I get the following:
I can't figure out how to implent this in code. I am wanting to fire the event when a user clicks a button. The only way I can find to get it working is by changing the above to:
And then, on button click:
However, the problem with this approach is that I don't want GameClicked to be
Public
. However, if I change it to Internal
it won't compile due to the INterface needing implementing again..
I know I'm getting things mixed up in terms of the core ideas and have done lots of reading around Delegates etc, but just getting more confused. What's the simplest way to achieve the above without the event being public?11 Replies
Interfaces describe the public-facing API of the class that implements it
Private and internal members are not a concern of an interface
Yes, but the problem relates to the scope of the event in the implementing class. My issue isn't around the interface iteself, per-se
I don't want GameClicked to be Public
seems to be your issue, no?thanks - yes
Then my answer stands
Non-public members are an implementation detail, and thus, not present in the interfaces
Hasn't answered my question at all, thanks
Don't have the event on the interface
I don't get it
The Event needs to be on the interface as it is used in a Presenter Class in MVP design.
Then it needs to be public
You CANNOT and SHOULD NOT have non-public members in an interface
So either delete the event from the interface
Or learn to live with the fact that it will be public
You have exactly those two choices
That is helpful, thanks ZZZZZ. Interesting though. I wonder why all members have to be public. In my example, I don't need the members of the class implementing the Interface to scope anything beyond the class library in which it sits. Hence, my looking to make it internal. 🤷
Because the interface defines the outer-facing members of the class
So that outside code knows what it can run
Outside code cannot access non-public members
So they're not an interface's concern