+ 2

I have questions about the delegates? Event? How this program works? Thanks)

public delegate void PriceChangedHandler(decimal oldPrice, decimal newPrice) ; public class Stock { string symbol ; decimal price; public Stock(string symbol) {this. symbol=symbol;} public event PriceChangedHandler PriceChenged; public decimal Price { get {return price;} set { if (price== value) return; decimal oldPrice = price; price = value; if (PriceChanged!=null) PriceChanged (oldPrice, price) ; } } }

25th Apr 2018, 6:08 PM
Đ’Đ°Đ»Đ”Ń€ĐžŃ
Đ’Đ°Đ»Đ”Ń€ĐžŃ - avatar
2 Answers
+ 1
Delegates are objects that basically can assume a function as a value. They are used as event handlers types because you can add methods to handle them in that way. Basically in that code you have that delegate object called PriceChangedHandler, that acts as an event handler for the actual event that is PriceChanged. So you see that our delegate can assume the value of a method that returns nothing (public delegate VOID) and that takes two decimals as parameters. If you go and see in the Price property, then, you see in it's setter that when the PriceChanged event is raised, it is first checked if it has assumed a value (if (PriceChanged != null) {...}), because it is indeed an object, and like any object it can be null. Only thing is, the value it assumes is, again, a method. Another thing, you see that when raising the event they just write the name of the event and pass it the parameters. That syntax just implies the call of the Invoke() method of the event, which is the method that runs the event handlers methods (the ones that registered for handling that particular event). You may write it in this way: if (PriceChanged != null) PriceChanged.Invoke(old price, price); So you see that PriceChanged is an object that has actual method, properties exc..., and it's type is a delegate called PriceChangedHandler.
25th Apr 2018, 6:36 PM
Mante
Mante - avatar
+ 1
Another way to raise the event if it has registered handlers would be to use the '?' operator. This operator checks if the left operand is null: if it isn't, it executes the right statement. So we would have: PriceChanged?.Invoke(oldPrice, price); To register a method as an event handler, you can use the '+=' operator. This way, you can add more methods to handle the event. You can use the '=' operator too, to add just one. myObject.PriceChanged += MyPriceChangedHandler; Where MyPriceChangedHandler is a method (or a lambda function) that has the same parameter and return type as the delegate that handles the event (PriceChangedHandler in this case) A delegate that is usually used to create events, especially in UWP, WPF and windows forms app's, is EventHandler. It takes two arguments, one is the object that raised the event, and the other is the event argument. The latter can be changed, tho. public EventHandler MyEvent1; MyEvent1?.Invoke(null, null); This creates a simple event of type EventHandler, that takes the sender and the event args as it's parameters. I suggest you to try creating events and delegates, so you can see them work, when and why they give errors and such. Practice is the best way of learning. Hope this was helpful and comprehensible enough. Btw, I had to split everything in two messages, as it was too long :)
25th Apr 2018, 6:37 PM
Mante
Mante - avatar