delegates

Create empty C# event handlers automatically

半腔热情 提交于 2019-12-17 10:15:01
问题 It is not possible to fire an event in C# that has no handlers attached to it. So before each call it is necessary to check if the event is null. if ( MyEvent != null ) { MyEvent( param1, param2 ); } I would like to keep my code as clean as possible and get rid of those null checks. I don't think it will affect performance very much, at least not in my case. MyEvent( param1, param2 ); Right now I solve this by adding an empty inline handler to each event manually. This is error prone, since I

Passing Data Between 3 Separate WinForms [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-17 10:01:31
问题 This question already has answers here : Communicate between two windows forms in C# (12 answers) Closed 9 months ago . Throughout my time coding in the realm of C# WinForms, I have had many instances in which I have come across different methods of passing data between forms. I work on a large codebase -- some of these methods were written by others, which I subsequently extended, and others were written by myself. It seems there are two main paradigms, both of which I have coded rather

C# Delegate Instantiation vs. Just Passing the Method Reference

会有一股神秘感。 提交于 2019-12-17 09:57:15
问题 I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference? What I mean is: Why do: Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod)); When you can do: Thread t = new Thread(SomeObject.SomeMethod); Both will compile and work in my experience...am I missing something? 回答1: As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is

C# Delegate Instantiation vs. Just Passing the Method Reference

拟墨画扇 提交于 2019-12-17 09:57:02
问题 I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference? What I mean is: Why do: Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod)); When you can do: Thread t = new Thread(SomeObject.SomeMethod); Both will compile and work in my experience...am I missing something? 回答1: As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is

Generating Delegate Types dynamically in C#

陌路散爱 提交于 2019-12-17 09:52:39
问题 We have a requirement where we need to generate delegate types on the fly. We need to generate delegates given the input parameters and the output. Both input and output would be simple types. eg, we need to generate int Del(int, int, int, string) and int Del2(int, int, string, int) Any pointers on how to get started on this would be very helpful. We need to parse formulate which are represented as xml. For example, we represent (a + b) as <ADD> <param type="decimal">A</parameter> <param type

Does jQuery have a handleout for .delegate('hover')?

流过昼夜 提交于 2019-12-17 07:17:20
问题 I am trying to use: $('mydiv').delegate('hover', function() { $('seconddiv').show(); }, function() { //For some reason jQuery won't run this line of code $('seconddiv').hide(); }); 回答1: User113716's great answer will no longer work in jQuery 1.9+ , because the pseudo-event hover is no longer supported (upgrade guide). Also since jQuery 3.0 delegate() for binding events is officially deprecated, so please use the new on()(docs) for all event binding purposes. You can easily migrate user113716

How to correctly unregister an event handler

十年热恋 提交于 2019-12-17 07:07:42
问题 In a code review, I stumbled over this (simplified) code fragment to unregister an event handler: Fire -= new MyDelegate(OnFire); I thought that this does not unregister the event handler because it creates a new delegate which had never been registered before. But searching MSDN I found several code samples which use this idiom. So I started an experiment: internal class Program { public delegate void MyDelegate(string msg); public static event MyDelegate Fire; private static void Main

How do I share an object between UIViewControllers on iPhone?

本秂侑毒 提交于 2019-12-17 06:26:29
问题 My application is a tab bar application, with a separate view controller for each tab. I have an object in my first view controller (A) which contains all my stored application data (Please ignore NSUserDefaults for this) which needs to be accessed by the second view controller (B) when I press a button on it. How can I achieve this in the best way? 回答1: One option you have is to declare your date model as instance variables of your app delegate (as mentioned by other commenters). Instead of

Removing event handlers

一曲冷凌霜 提交于 2019-12-17 06:11:51
问题 Is this: Button.Click -= new EventHandler(Button_Click); the same as this: Button.Click -= Button_Click; I ask because to me it seems that the former is removing a new reference to a method, and the latter one is removing a method itself. But then again, maybe the new EventHandler part is implicit in the += or -= overload in case the programmer doesn't explicitly assign it like that? In case it is different how about Button.Click -= new EventHandler(Button_Click); VS Button.Click -= Button

C# pattern to prevent an event handler hooked twice [duplicate]

自古美人都是妖i 提交于 2019-12-17 05:36:13
问题 This question already has answers here : How to ensure an event is only subscribed to once (5 answers) Closed 4 years ago . Duplicate of: How to ensure an event is only subscribed to once and Has an event handler already been added? I have a singleton that provides some service and my classes hook into some events on it, sometimes a class is hooking twice to the event and then gets called twice. I'm looking for a classical way to prevent this from happening. somehow I need to check if I've