C
C#β€’2y ago
wcasa

βœ… delegate and event IMPLEMENTATION question..

why do we have to create separate class for delegates/events?
31 Replies
Thinker
Thinkerβ€’2y ago
Wdym class? A delegate is a delegate
wcasa
wcasaOPβ€’2y ago
ive noticed that people declare delegate in separate class
wcasa
wcasaOPβ€’2y ago
like here
wcasa
wcasaOPβ€’2y ago
delegate (with event) in separate class, other classes', main function in other class
Thinker
Thinkerβ€’2y ago
Well typically you declare delegates in the same class that you declare the event in, if the delegate is tightly connected to the event specifically. If you look at Func<> and Action<> though, they're just defined inside the System namespace.
wcasa
wcasaOPβ€’2y ago
well, why, do we declare event and delegate in separate class? why wouldnt you make it global?
Thinker
Thinkerβ€’2y ago
Well firstly you can't make an event global (you can make it static though)
wcasa
wcasaOPβ€’2y ago
why???
Thinker
Thinkerβ€’2y ago
because events are type members i.e. they have to be declared inside a type
wcasa
wcasaOPβ€’2y ago
where is it best to declare event?
Thinker
Thinkerβ€’2y ago
And also because global events imply some kind of global state, which is generally bad
wcasa
wcasaOPβ€’2y ago
A)creating separate class for event/delegate or B)in the class which's object is connected to event once again, dude literally created separate class for event, instead of implementing it in the Video class (he had Video class which had only 1 property, just for sake of example) btw, why wouldnt he implement Encode method inside Video class?
Thinker
Thinkerβ€’2y ago
Doesn't seem like that class is only for the sake of the event
wcasa
wcasaOPβ€’2y ago
event and encoding** you are correct
Thinker
Thinkerβ€’2y ago
I assume Video is meant to contain data about a single video, so wouldn't make sense to have an event there
wcasa
wcasaOPβ€’2y ago
???? what is wrong with:
class Video{
//properties of video
public string Title {get; set;}
public delegate void VideoEncoderHandler(object sender, EventArgs e)
public event VideoEncoderHandler VideoEncoded;
public void Encode(this){
//implementation of it from screenshot
}
}
class Video{
//properties of video
public string Title {get; set;}
public delegate void VideoEncoderHandler(object sender, EventArgs e)
public event VideoEncoderHandler VideoEncoded;
public void Encode(this){
//implementation of it from screenshot
}
}
why would he separate encode from the video class, that doesnt make any sense
Thinker
Thinkerβ€’2y ago
What if you want to create a video but don't care about encoding it?
wcasa
wcasaOPβ€’2y ago
you can just dont use this method catthinking
Thinker
Thinkerβ€’2y ago
What if the encoder needs some internal state?
wcasa
wcasaOPβ€’2y ago
wdym by internal state?
Thinker
Thinkerβ€’2y ago
For instance, the encoder needs a list as a field to keep track of tasks or whatever. If you put that in Video, then suddenly what is meant to just be data about a video contains state which isn't related to the video at all.
wcasa
wcasaOPβ€’2y ago
oh that makes sense πŸ‘ ooorrr not you can pass it as parameter and dont include it in the properties (??)
Thinker
Thinkerβ€’2y ago
yep, which generally leads to a lot cleaner and more maintainable code The video doesn't have to worry about the logic of encoding, that's up to another class.
wcasa
wcasaOPβ€’2y ago
so, creating this methodo in the video class is better?
Thinker
Thinkerβ€’2y ago
no, again separating it into another class is generally better And besides, I have no idea why Encode is void and for some reason needs an event. If I was making this this is certainly not how I would do it, but then again it's just for demonstration.
wcasa
wcasaOPβ€’2y ago
nooo i mean, passing external data in the parameters, instead of using it as properties it's just and example alright, in conclusion, i have to declare delegate / event inside the class that calls delegate / event? nooo man, keep typing
Axiss
Axissβ€’2y ago
freeCodeCamp.org
SOLID Definition – the SOLID Principles of Object-Oriented Design E...
The SOLID design principles help us create maintainable, reusable, and flexible software designs. Each letter in the acronym SOLID stands for a specific principle. Here is what each letter in the acronym stands for: * S: Single responsibility principle. * O: Open–closed principle. * L: Liskov
Thinker
Thinkerβ€’2y ago
You don't have to declare the delegate in the same type as the event, although if the delegate is specifically meant for the event then it's a good idea to keep everything in one place.
wcasa
wcasaOPβ€’2y ago
okie, thanks! are principles and patterns same thing?
Axiss
Axissβ€’2y ago
Not really but there is some overlap. I wouldn't get hung up on how they are named. If I had to distill the differences I would say principles are a higher level concept, patterns are how you implement the principles in your programming language of choice.
wcasa
wcasaOPβ€’2y ago
understandable, thanks!

Did you find this page helpful?