anonymous-methods

anonymous function variable scope [js, ajax]

$(".delete").click( function() { var thesender = this; $(thesender).text("Del..."); $.getJSON("ajax.php", {}, function(data) { if (data["result"]) $(thesender).remove(); // variable defined outside else alert('Error!'); ...

Why can't c# use inline anonymous lambdas or delegates?

I hope I worded the title of my question appropriately. In c# I can use lambdas (as delegates), or the older delegate syntax to do this: Func<string> fnHello = () => "hello"; Console.WriteLine(fnHello()); Func<string> fnHello2 = delegate() { return "hello 2"; }; Console.WriteLine(fnHello2()); So why can't I "inline" the lambda o...

Why is one Func valid and the other (almost identical) not.

private static Dictionary<Type, Func<string, object>> _parseActions = new Dictionary<Type, Func<string, object>> { { typeof(bool), value => {Convert.ToBoolean(value) ;}} }; The above gives an error Error 14 Not all code paths return a value in lambda expression of type 'System...

Why won't my anonymous function fire on grid.prerender?

In my gridview I have fields for inserting a new record in the footer. In my objectdatasource selecting event if no records came back I bind a single mock row to force the footer to show so they can still add records. Since the row does not contain real data I hide the row. ... If result.ItemCount = 0 Then result = mock...

modify a variable in a anonymous method

I want to modify a local variable in a function of extension method. See int myvar=0; MyList.Where( x => { if (condition) myvar += 1; return false; }); return myvar; Why that is not working? ...

Scoping inside Javascript anonymous functions

I am trying to make a function return data from an ajax call that I can then use. The issue is the function itself is called by many objects, e.g.: function ajax_submit (obj) { var id = $(obj).attr('id'); var message = escape ($("#"+id+" .s_post").val ()); var submit_string = "action=post_message&message="+message; ...

Predicate<int> match question

Hi, I do not understand how following code works. Specifically, I do not understand using of "return i<3". I would expect return i IF its < than 3. I always though that return just returns value. I could not even find what syntax is it. Second question, it seems to me like using anonymous method (delegate(int i)) but could be possible t...

Anonymous method as function result

What I want to do is to assign an anonymous method which I get as a function result to a variable of the same type. Delphi complains about not beeing able to do the assignement. Obviously Delphi things I want to assign the "GetListener" function instead of the result of that same function. Any help with this is very much appreciated. ...

How to do this in VB 2010 (C# to VB conversion)

I would like to have the following to be translated to VB 2010 (with advanced syntaxes) _domainContext.SubmitChanges( submitOperation => { _domainContext.Load<Customer>( _domainContext.GetCustomersQuery(), LoadBehavior.RefreshCurrent, loadOperation => { ...

Scope of variables inside anonymous functions in C#

I have a doubt in scope of varibles inside anonymous functions in C#. Consider the program below: delegate void OtherDel(int x); public static void Main() { OtherDel del2; { int y = 4; del2 = delegate { Console.WriteLine("{0}...

What's keeping this timer in scope? The anonymous method?

Ok, So I have a method which fires when someone clicks on our Icon in a silverlight application, seen below: private void Logo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { e.Handled = true; ShowInfo(true); DispatcherTimer autoCloseTimer = new DispatcherTimer(); autoCloseTimer.Inter...

MethodInfo.Invoke with a delegate inside Object[] params

Let me try to give a little example. class Session ( public delegate string CleanBody(); public static void Execute(string name, string q, CleanBody body) ... can be used like: Session.Execute("foo", "bar", delegate() { string x="beep"; /* whatever*/ return x; }); But what if I need to run is via MethodInfo.Invoke -- as in ...

Trying to get VB anonymous methods working. querying lists

I am trying to get my code working as per the instruction on http://www.paulstovell.com/vb-anonymous-methods So far I have the wrapper: Public Delegate Function PredicateWrapperDelegate(Of T, A)(ByVal item As T, ByVal argument As A) As Boolean Public Class PredicateWrapper(Of T, A) Private _argument As A Private _wrapperDelegat...

Is there a useful design pattern for chained asynchronous/event calls?

I'm currently having to integrate a lot of web service calls inside Silverlight that make calls similar to the code below. No user interaction should be possible until the loading of all 3 is complete. // In my view, having standard delegate methods (non-anonymous) makes the // code below even messier. // I've left out the EventArgs t...

C#, simplified code to handle both changes and updates of a dependency property

Obviously, I'm not an expert in C#. I would like to simplify this code by using an anonymous handler, or maybe a lambda, not sure. ValueHasChanged is a PropertyChangedCallback used when a dp is changed, it ensures the new object will be monitored for update, so that both changes and updates will be processed using the same code: ProcessN...

Why I cannot use the anonymous method like that?

Hi, Why can't I have this? I mean it would spare a delegate declaration: int X=delegate int(){...}; I know it can be done this way: delegate int IntDelegate(); ... IntDelegate del=delegate(){return 25;}; int X=del(); ...

Anonymous Methods too much of a good thing? (C#)

Greetings, I'm working in a code base that uses alot of anonymous methods, where the anonymous methods are chaining other anonymus methods that call the same thing the first one calls. main() { anonymous1(); } anonymous1() { // call anonymous2 } anonymous2() { //call anonymous3 } anonymous3() { // Call anonymous1 } thats th...

What problems are there using generics and anonymous methods in Delphi 2009?

I'd like to start using generics and anonymous method, mainly to learn what that's all about and why I would want to use them. Having Delphi 2009, I often read that generics and anonymous methods are not completely implemented or buggy, which was fixed in Delphi 2010. I would like to avoid having to wonder if it's my fault or a bug in ...

Weird idea: C# - declare a method insinde another method.

Okay, in python one can do this: def foo(monkeys): def bar(monkey): #process and return new monkey processed_monkeys = list() for monkey in monkeys: processed_monkeys += bar(monkey) return processed_monkeys (This is just a stupid example) I sometimes miss this functionality of declaring a method inside ...

Confusion about anonymous methods parameters.

While learning on anonymous methods, i've found the following example on the internet: namespace AnonymousMethods { public class MyClass { public delegate void MyDelegate(string message); //delegate accepting method with string parameter public event MyDelegate MyEvent; public void RaiseMyEvent(string ms...