anonymous-methods

VCL events with anonymous methods - what do you think about this implementation?

為{幸葍}努か 提交于 2019-11-30 11:29:11
Since anonymous methods appeared in Delphi I wanted to use them in VCL components events. Obviously for backward compatibility the VCL wasn't updated, so I managed to make a simple implementation with a few caveats. type TNotifyEventDispatcher = class(TComponent) protected FClosure: TProc<TObject>; procedure OnNotifyEvent(Sender: TObject); public class function Create(Owner: TComponent; Closure: TProc<TObject>): TNotifyEvent; overload; function Attach(Closure: TProc<TObject>): TNotifyEvent; end; implementation class function TNotifyEventDispatcher.Create(Owner: TComponent; Closure: TProc

anonymous delegates in C#

此生再无相见时 提交于 2019-11-30 10:14:19
问题 I can't be the only one getting tired of defining and naming a delegate for just a single call to something that requires a delegate. For example, I wanted to call .Refresh() in a form from possibly other threads, so I wrote this code: private void RefreshForm() { if (InvokeRequired) Invoke(new InvokeDelegate(Refresh)); else Refresh(); } I'm not even sure I have to, I just read enough to be scared that it won't work at some later stage. InvokeDelegate is actually declared in another file, but

Scope of anonymous methods

喜夏-厌秋 提交于 2019-11-30 07:03:27
One nice thing about anonymous methods is that I can use variables that are local in the calling context. Is there any reason why this does not work for out-parameters and function results? function ReturnTwoStrings (out Str1 : String) : String; begin ExecuteProcedure (procedure begin Str1 := 'First String'; Result := 'Second String'; end); end; Very artificial example of course, but I ran into some situations where this would have been useful. When I try to compile this, the compiler complains that he "cannot capture symbols". Also, I got an internal error once when I tried to do this. EDIT I

Anonymous method as parameter to BeginInvoke?

烈酒焚心 提交于 2019-11-30 06:45:40
Why can't you pass an anonymous method as a parameter to the BeginInvoke method? I have the following code: private delegate void CfgMnMnuDlg(DIServer svr); private void ConfigureMainMenu(DIServer server,) { MenuStrip mnMnu = PresenterView.MainMenu; if (mnMnu.InvokeRequired) { mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu, new object[] { server}); } else { // Do actual work here } } I'm trying to avoid declaring the delegate. Why can't I write something like the below instead? Or can I, and I just can't figure out the correct syntax? The below currently generates an: Argument type

ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method

筅森魡賤 提交于 2019-11-30 04:35:44
Passing two parameters to a new thread on the threadpool can sometimes be complicated, but it appears that with lambda expressions and anonymous methods, I can do this: public class TestClass { public void DoWork(string s1, string s2) { Console.WriteLine(s1); Console.WriteLine(s2); } } try { TestClass test = new TestClass(); string s1 = "Hello"; string s2 = "World"; ThreadPool.QueueUserWorkItem( o => test.DoWork(s1, s2) ); } catch (Exception ex) { //exception logic } Now, I've certainly simplified this example, but these points are key: The string objects being passed are immutable and

Compiler generated incorrect code for anonymous methods [MS BUG FIXED]

别来无恙 提交于 2019-11-30 04:22:05
See the following code: public abstract class Base { public virtual void Foo<T>() where T : class { Console.WriteLine("base"); } } public class Derived : Base { public override void Foo<T>() { Console.WriteLine("derived"); } public void Bang() { Action bang = new Action(delegate { base.Foo<string>(); }); bang(); //VerificationException is thrown } } new Derived().Bang(); throws an exception. Inside the generated CIL of the method Bang I got: call instance void ConsoleApp.Derived::'<>n__FabricatedMethod1'<string>() and the signature of the compiler generated method: method private hidebysig

Closures in C# event handler delegates? [duplicate]

十年热恋 提交于 2019-11-30 03:08:00
This question already has an answer here: Captured variable in a loop in C# 8 answers I am coming from a functional-programming background at the moment, so forgive me if I do not understand closures in C#. I have the following code to dynamically generate Buttons that get anonymous event handlers: for (int i = 0; i < 7; i++) { Button newButton = new Button(); newButton.Text = "Click me!"; newButton.Click += delegate(Object sender, EventArgs e) { MessageBox.Show("I am button number " + i); }; this.Controls.Add(newButton); } I expected the text "I am button number " + i to be closed with the

DataTable.Select vs DataTable.rows.Find vs foreach vs Find(Predicate<T>)/Lambda

杀马特。学长 韩版系。学妹 提交于 2019-11-30 01:50:16
I have a DataTable/collection that is cached in memory, I want to use this as a source to generate results for an auto complete textbox (using AJAX of course). I am evaluating various options to fetch the data quickly. The number of items in the collection/rows in the datatable could vary from 10000 to 2,000,000. (So that we dont get diverted, for the moment assume that the decision has been made, I have ample RAM and I will be using the cache and not database query for this) I have some additional business logic for this processing; I have to prioritize the auto complete list as per a

How and when are variables referenced in Delphi's anonymous methods captured?

大兔子大兔子 提交于 2019-11-29 23:04:35
This was prompted by How to compare TFunc/TProc containing function/procedure of object? , specifically by David's comment to Barry's question. Since I don't have a Blog to post this to I'm going to ask this question here, and answer it. Question : When and how are variables referenced in Delphi's anonymous methods captured? Example: procedure ProcedureThatUsesAnonymousMethods; var V: string; F1: TFunc<string>; F2: TFunc<string>; begin F1 := function: string begin Result := V; // references local variable end V := '1'; F2 := function: string begin Result := V; end V := '2'; ShowMessage(F1);

anonymous delegates in C#

自古美人都是妖i 提交于 2019-11-29 19:33:44
I can't be the only one getting tired of defining and naming a delegate for just a single call to something that requires a delegate. For example, I wanted to call .Refresh() in a form from possibly other threads, so I wrote this code: private void RefreshForm() { if (InvokeRequired) Invoke(new InvokeDelegate(Refresh)); else Refresh(); } I'm not even sure I have to, I just read enough to be scared that it won't work at some later stage. InvokeDelegate is actually declared in another file, but do I really need an entire delegate dedicated just for this? aren't there any generic delegates at all