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
wcasaβ€’2y ago
ive noticed that people declare delegate in separate class
wcasa
wcasaβ€’2y ago
like here
wcasa
wcasaβ€’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
wcasaβ€’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
wcasaβ€’2y ago
why???
Thinker
Thinkerβ€’2y ago
because events are type members i.e. they have to be declared inside a type
wcasa
wcasaβ€’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
wcasaβ€’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
wcasaβ€’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
wcasaβ€’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
wcasaβ€’2y ago
you can just dont use this method catthinking
Thinker
Thinkerβ€’2y ago
What if the encoder needs some internal state?
wcasa
wcasaβ€’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
wcasaβ€’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
wcasaβ€’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
wcasaβ€’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
wcasaβ€’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
wcasaβ€’2y ago
understandable, thanks!
Want results from more Discord servers?
Add your server
More Posts
❔ Sorting a JObject by child valuesI have JSON being returned by an API that I'm trying to sort to find the most relevant result. Here'❔ Visual Studio build solution problemHi, i just finished writing my c# console app with .Net Core but i cant run it in other folder thats❔ Discord.Net autocompletion VSCodeHey guys! I want to program a Discord Bot but when i try to write something on my script i get no toβœ… Can you add related data to a DbContext like thisI have these classes where Company.Locations navigates to a Locations table and Location.LocationRev❔ Array Question (solved)I have a question. I cant tell if I'm being stupid, but I want to use an array like for example privGetting a string to only accept 0's and 1'sI don't understand what is wrong with this line of code in particular: bool result = userInput.All(cβœ… βœ… Generate migrations with EF on sqlserver image docker taking too much timeHello , it's my first time making questions here , so if i forgot to provide some information , plea❔ Read Stream to End of Json ObjectI am trying to read a stream from the beginning of a json object until the end of a json object, wit❔ Identity and Authentication Advice or SuggestionsI'm building a Hosted Blazor WASM website with .Net 6 Backend API and I'm struggling to make a decis❔ Confused about Assembly.Load (Hotloading CSharp Assemblies)I want to be able to load/unload user made C# libraries during runtime. However, these user librarie