C
C#•7mo ago
stigzler

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:
event EventHandler<IGame> GameClicked;
event EventHandler<IGame> GameClicked;
When I do "Implement reminaing memebers explicity" (I often add to my inerface as I go) I get the following:
event EventHandler<IGame> IGamesView.GameClicked
{
add
{
throw new NotImplementedException();
}

remove
{
throw new NotImplementedException();
}
}
event EventHandler<IGame> IGamesView.GameClicked
{
add
{
throw new NotImplementedException();
}

remove
{
throw new NotImplementedException();
}
}
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:
public event EventHandler<IGame> GameClicked;
public event EventHandler<IGame> GameClicked;
And then, on button click:
GameClicked.Invoke(sender, (IGame)e.Item);
GameClicked.Invoke(sender, (IGame)e.Item);
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
Angius
Angius•7mo ago
Interfaces describe the public-facing API of the class that implements it Private and internal members are not a concern of an interface
stigzler
stigzler•7mo ago
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
Angius
Angius•7mo ago
I don't want GameClicked to be Public
seems to be your issue, no?
stigzler
stigzler•7mo ago
thanks - yes
Angius
Angius•7mo ago
Then my answer stands Non-public members are an implementation detail, and thus, not present in the interfaces
stigzler
stigzler•7mo ago
Hasn't answered my question at all, thanks
phaseshift
phaseshift•7mo ago
Don't have the event on the interface I don't get it
stigzler
stigzler•7mo ago
The Event needs to be on the interface as it is used in a Presenter Class in MVP design.
Angius
Angius•7mo ago
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
stigzler
stigzler•7mo ago
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. 🤷
Angius
Angius•7mo ago
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