delegates

Is there a Delegate which isn't a MulticastDelegate in C#?

别来无恙 提交于 2019-12-18 04:49:08
问题 I think the answer is NO? If there isn't, why do we have separated Delegate and MulticastDelegate classes? Maybe it's again because of "some other .NET languages"? 回答1: EDIT: I thought this was part of ECMA 335, but I can't see it in there anywhere. You can't create such a delegate type in C#, but you can in IL: .class public auto ansi sealed Foo extends [mscorlib]System.Delegate { // Body as normal } The C# compiler has no problems using such a delegate: using System; class Test { static

How can I pass an F# delegate to a P/Invoke method expecting a function pointer?

喜夏-厌秋 提交于 2019-12-18 04:49:06
问题 I'm attempting to set up a low-level keyboard hook using P/Invoke in an F# application. The Win32 function SetWindowsHookEx takes a HOOKPROC for its second argument, which I've represented as a delegate of (int * IntPtr * IntPtr) -> IntPtr , similar to how this would be handled in C#. When calling the method, I get a MarshalDirectiveException stating that the delegate parameter cannot be marshaled because Generic types cannot be marshaled I'm not sure how generics are involved, as all types

C# compile error: “Invoke or BeginInvoke cannot be called on a control until the window handle has been created.”

折月煮酒 提交于 2019-12-18 04:11:07
问题 I just posted a question about how to get a delegate to update a textbox on another form. Just when I thought I had the answer using Invoke...this happens. Here is my code: Main Form Code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Data.OleDb; using System.Collections.Specialized; using System.Text; using System.Threading; delegate void logAdd(string message); namespace

BeginInvoke throws exception

有些话、适合烂在心里 提交于 2019-12-18 04:07:27
问题 I have the following problem. FindRoot is actually in a third party dll and I do not have control over it. It has to be called via Begin invoke . Sometimes, the FindRoot method throws exception. This causes my whole application to crash. Now how do I prevent my application from crashing even if FindRoot throws exception. delegate void AddRoot(double number); public static void FindRoot(double number) { throw new Exception();/// sometimes is thrown. } static void back_DoWork(object sender,

Should you set the delegate to nil in the class using the delegate or in the class itself

本秂侑毒 提交于 2019-12-18 03:59:06
问题 If class A is using class B and class A is class B's delegate, is it ok if the delegate is set to nil in class B's dealloc? I have seen code usually resetting the delegate to nil inside class A's dealloc but wasn't sure the real difference doing it one way or the other. e.g. This is the usual way: // somewhere in class A - (void) someFunc { self.b = [[B alloc] init]; self.b.delegate = self; } - (void) dealloc { self.b.delegate = nil; [self.b release]; } 回答1: Yes, you should set the classB's

Function pointers/delegates in Java?

无人久伴 提交于 2019-12-18 03:19:10
问题 For my Java game server I send the Action ID of the packet which basically tells the server what the packet is for. I want to map each Action ID (an integer) to a function. Is there a way of doing this without using a switch? 回答1: What about this one? HashMap<Integer, Runnable> map = new HashMap<Integer, Runnable>(); map.put(Register.ID, new Runnable() { public void run() { functionA(); } }); map.put(NotifyMessage.ID, new Runnable() { public void run() { functionB(); } }); // ... map.get(id)

Create a delegate from a property getter or setter method

两盒软妹~` 提交于 2019-12-18 03:10:25
问题 To create a delegate from a method you can use the compile type-safe syntax: private int Method() { ... } // and create the delegate to Method... Func<int> d = Method; A property is a wrapper around a getter and setter method, and I want to create a delegate to a property getter method. Something like public int Prop { get; set; } Func<int> d = Prop; // or... Func<int> d = Prop_get; Which doesn't work, unfortunately. I have to create a separate lambda method, which seems unnecessary when the

Achieve button click in UICollectionView

青春壹個敷衍的年華 提交于 2019-12-18 02:57:41
问题 Is there a way I can get the button click event from a button inside a UICollectionViewCell ? I used a nib to populate the collection view, the cell has the button but its action is not getting called. I think the problem is with the delegate being called. How can I fix this? How I created : Added an empty nib, created a collection view cell Added a .h and .m file and made the cell nib's files owner as the class created Wrote an action in the class. Connected the button to the action Is there

Threads and delegates — I don't fully understand their relations

只谈情不闲聊 提交于 2019-12-17 23:39:16
问题 I wrote a code that looks somewhat like this: Thread t = new Thread(() => createSomething(dt, start, finish) ); t.Start(); And it works (sometimes it almost feel like there are multiple threads). Yet I don't use any delegates. What is the meaning of a tread without a delegate? If a delegate is necessary — then please tell me what and how the connection is made to the delegate. 回答1: Multi-threading is very complex. You are cutting and pasting code without even learning anything about the most

In Swift, how do I have a UIScrollView subclass that has an internal and external delegate?

﹥>﹥吖頭↗ 提交于 2019-12-17 23:14:59
问题 I'm subclassing UIScrollView to add some features such as double tap to zoom and an image property for gallery purposes. But in order to do the image part my subclass has to be its own delegate and implement the viewForZoomingInScrollView . But then when someone uses my scroll view subclass, they might like to get delegate notifications as well to see scrollViewDidScroll or what have you. In Swift, how do I get both of these? 回答1: Here is a Swift version of this pattern: Although