delegate

a constructor as a delegate - is it possible in C#?

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a class like below: class Foo { public Foo(int x) { ... } } and I need to pass to a certain method a delegate like this: delegate Foo FooGenerator(int x); Is it possible to pass the constructor directly as a FooGenerator value, without having to type: delegate(int x) { return new Foo(x); } ? EDIT: For my personal use, the question refers to .NET 2.0, but hints/responses for 3.0+ are welcome as well. 回答1: Nope, the CLR does not allow binding delegates to ConstructorInfo . You can however just create your own: static T Make (Action init

pyserial serialwin32.py has attribute error

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm new to Python and trying to run a demo provide by Invensense. They provide a Python client which should take COM traffic and manipulate a graphic. The demo seems to crash out of the box and I'm not sure if I have something wrong with pyserial that I installed. The requirements were python2.7, pyserial and pygame. The pyserial executable serialwin32.py throws this error: Traceback (most recent call last): ....... File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 47, in open if port.upper().startswith('COM') and int(port[3:]

用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树

谁说我不能喝 提交于 2019-12-03 02:22:34
  这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力。曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托,表达式树这些应用。今天我尝试用简单的方法叙述一下,让大家在五分钟内看完这篇博客。 第一分钟:委托   有些教材,博客说到委托都会提到事件,虽然事件是委托的一个实例,但是为了理解起来更简单,今天只谈委托不谈事件。先上一段代码: 下边的代码,完成了一个委托应用的演示。一个委托分三个步骤: public partial class WebForm3 : System.Web.UI.Page { //step01:首先用delegate定义一个委托 。 public delegate int CalculatorAdd(int x, int y); protected void Page_Load(object sender, EventArgs e) { //step03:用这个方法来实例化这个委托。 CalculatorAdd cAdd = new CalculatorAdd(Add); //int result = cAdd(5, 6); int result = cAdd.Invoke(5,6); } // step02:声明一个方法来对应委托。 public int Add(int x, int y) { return x + y; }

The app delegate must implement the window property if it wants to use a main storyboard file

匿名 (未验证) 提交于 2019-12-03 02:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm programmatically implement a list view controller. When I try to run the project, I got error: 2012-11-07 22:46:34.719 myTableViewControl[12021:c07] The app delegate must implement the window property if it wants to use a main storyboard file. 2012-11-07 22:46:34.722 myTableViewControl[12021:c07] -[AppDelegate setWindow:]: unrecognized selector sent to instance 0x7674e70 2012-11-07 22:46:34.723 myTableViewControl[12021:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setWindow:]:

Weak events in .NET?

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: If object A listens to an event from object B, object B will keep object A alive. Is there a standard implementation of weak events that would prevent this? I know WPF has some mechanism but I am looking for something not tied to WPF. I am guessing the solution should use weak references somewhere. 回答1: Dustin Campbell from the DidItWith.NET blog examines several of the failed attempts to create weak event handlers, then goes on to show a valid, working, lightweight implementation: Solving the Problem With Weak Event Handlers . Ideally,

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

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 delegate property to nil in classA's dealloc. It's

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

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 delegate property to nil in classA's dealloc. It's

How to dismiss keyboard iOS programmatically

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I created a UITextField programmatically making the UITextField a property of the viewController. I need to dismiss the keyboard with the return and the touch on the screen. I was able to get the screen touch to dismiss, but pressing return is not working. I've seen how to do it with storyboards and by allocating and initializing the UITextField object directly without creating it as a property. Possible to do? I am a noob - sorry! .h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> @property (strong

How to subclass UIScrollView and make the delegate property private

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here is what I want to achieve: I want to subclass an UIScrollView to have additional functionality. This subclass should be able to react on scrolling, so i have to set the delegate property to self to receive events like: - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView { ... } On the other hand, other classes should still be able to receive these events too, like they were using the base UIScrollView class. So I had different ideas how to solve that problem, but all of these are not entirely satisfying me :( My main

no overload for matches delegate &#039;system.eventhandler&#039;

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: As I'm pretty new to C#, I struggle with the following piece of code. When I click to button 'knop', the method 'klik' has to be executed. The method has to draw the Bitmap 'b', generated by 'DrawMandel' on the form. But I constantly get the error 'no overload for matches delegate 'system.eventhandler'. using System; using System.Windows.Forms; using System.Drawing; class Mandelbrot : Form { public Bitmap b; public Mandelbrot() { Button knop; knop = new Button(); knop.Location = new Point(370, 15); knop.Size = new Size(50, 30); knop.Text =