Hello guys, I've started teaching myself C#, and am struggling to get my head around some code, specifically relating to delegates/events, and am looking for some advice on if I'm understanding them...
So I have this line:
public delegate void myEventHandler(object source, myEventArgs e);
Which I understand defines the shape of a method, that can be used as a pointer to a function. I also understand the myEventArgs part, so wont type that out!
Then, later in the code, I have a class that has this code:
public event myEventHandler onGetString;
public void receiveString(string aString)
onGetString(this, new myEventArgs(aString));
Now, I understand this as saying whenever the receiveString functon is called, it will fire the onGetString event (where some stuff happens in myEventArgs class).
HOWEVER....this is where my brain stops working....so, in my main class, I have this code:
myClass myClassInstance = new myClass();
myClassInstance.onGetString += new myEventHandler(dealWithString);
public void dealWithString(object source, myEventArgs e)
NOW, my problem is this; does that mean that, whenever myClass calls onGetString (when onReceiveString is called), it will also call dealWithString in the main class? I hope that makes sense? I didn't write this code, I'm just trying to understand it!! Does it mean I can basically use += to keep adding custom methods on top of onGetString? So whenever onGetString gets called in myClass, all other classes that have added using += will also get their attached method called? Am I making ANY sense??