Events and Delegates [Answered]

I'm encountering a very strange error. I have a delegate and an event inside my IAuthService. Now when other class are trying to inherit from the interface. For some reason I have to implement the event explicitly? The first one is working fine! But the second one is complaining for some reason.
16 Replies
Monsieur Wholesome
Btw, put that delegate definition out of there It's not a class member, but kind of a type definition on its own
333fred
333fred2y ago
I mean, it can be Can you show the definition of IAuthService?
Monsieur Wholesome
It's not bound to an interface contract + every single implementing type would theoretically define the same delegate
SwaggerLife
SwaggerLife2y ago
looks like this
internal interface IAuthService
{
delegate void AccountCreatedEventHandler(object source, AccountCreatedEventArgs args);

event AccountCreatedEventHandler? AccountCreated;
}
internal interface IAuthService
{
delegate void AccountCreatedEventHandler(object source, AccountCreatedEventArgs args);

event AccountCreatedEventHandler? AccountCreated;
}
333fred
333fred2y ago
Yep, ok So, here's what's happening Your AccountCreated in AuthService has a return type of AuthService.AccountCreatedEventHandler What it actually needs to be is IAuthService.AccountCreatedEventHandler
SwaggerLife
SwaggerLife2y ago
Yes But I don't get it
333fred
333fred2y ago
Just straight up remove the AccountCreatedEventHandler delegate in AuthService AuthService.AccountCreatedEventHandler != IAuthService.AccountCreatedEventHandler Those are completely separate types
Monsieur Wholesome
When declaring a delegate, youre declaring it as if it was a class You declare it once, and now you can use that delegate It's not a per-class member
SwaggerLife
SwaggerLife2y ago
Yes it seems like I don't need to even have the delegate inside the AuthService. But I would be lying if I said I understood the event thing. AuthService.AccountCreatedEventHandler != IAuthService.AccountCreatedEventHandler I get that they don't have the same types. But for the other part it's still a bit confusing.
333fred
333fred2y ago
The other part?
SwaggerLife
SwaggerLife2y ago
I mean normally when I implement a method from an Interface. It doesn't force me to Implement it explicitly? This is the part which is confusing to me. Maybe I have misunderstood the entire concept.
333fred
333fred2y ago
You're not implementing explicitly You're saying "The return type is the AccountCreatedEventHandler defined in IAuthService"
Monsieur Wholesome
You defined the same delegate twice, now in the deriving implementing class you mentioned the delegate name And thus it prioritized the one from right below it, though the interface means the one from the interface That's why you had to explicitly mention "the one from the interface"
333fred
333fred2y ago
For explicit implementation, the Interface. goes on the method/property name, not on a type Explicit implementation would have looked like this:
event IAuthService.AccountCreatedEventHandler IAuthService.AccountCreated;
event IAuthService.AccountCreatedEventHandler IAuthService.AccountCreated;
SwaggerLife
SwaggerLife2y ago
Oh shit I get it know. Stupid me. Thank you very much guys.
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts