Timothy Khouri - SingingEels.com

I subscribe to the "take it apart and rebuild it" approach to learning.

Lambda Expressions to Replace Inline Delegates

I've taken a change from using 'inline-delegates' which were introduced in .NET 2.0 (in C# 2) to using Lambda expressions for those quick "one-liner" functions. The great thing is that you can do this in C# 3.0 or VB9! If you didn't already know, C# 3.0 and VB 9 are in the new release of the .NET 3.5 framework.

If you don't quite know what I'm talking about, or if you'd just like to see some examples, check out the post I just put on Eels: Lambda Expressions to Replace Inline Delegates

Comments

jw said:

Can you convert this to vb.net?

this.CancelButton.Click += (sender, args) => this.Response.Redirect("~/View.aspx", true);

# October 7, 2007 11:43 PM

Joe Chung said:

AddHandler CancelButton.Click, Function(src, args) Response.Redirect("~/View.aspx", True)

# October 8, 2007 1:34 AM

Joe Chung said:

Never mind the above.  I get a compiler error "Function does not return value" when I tried that.

Strangely enough, it compiled just fine when I tried this instead.

AddHandler CancelButton.Click, Function(src, args) CancelButton.Text = "Clicked"

# October 8, 2007 1:40 AM

AndrewSeven said:

Does the VB need a "Call" statement?

# October 8, 2007 10:52 AM

Panya said:

Chung,

The probelm is here

CancelButton.Text = "Clicked"

This = operator is treat as a test operator,

not an assignment operator.

I guess the Function() lambda in VB

is always expect something to return.

# October 21, 2007 1:04 PM

Steele Price said:

This is indeed a cool feature, but in VB9 this will not work because as Panya states, VB9 Lambdas must return a value.  The good news is that in VB10 we will get multi-line lambdas and Statement Lambdas.

Anywhere you would use a Sub in VB will not work as a Lambda, hence, you have to wait for VB10 to use this for Event Handlers such as CancelButton.Click

# December 31, 2008 12:14 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)