Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.


Lambda Unexpected Behavior

One item you strive to avoid when you design and implement a feature is unexpected behavior.  Unfortunately there is one case we couldn't avoid with Lambda's in VB9.  I just ran into the this problem when coding up a handler.  I wanted to disable a button when the text of particular TextBox was empty.  I wrote the following code to handle the situation.

AddHandler c.TextChanged, Function() okButton.Enabled = (0 <> c.Text.Length)

This doesn't quite do what I intended.  This instead will simply compare the two values. 

In VB9 Lambda Expressions are always an expression.  In version 9 of VB, there is no concept of an assignment as an expression.  There is only a statement version.  As a result this doesn't do anything useful. 

This has tripped up a few people along the way.  It's an unfortunate side effect of only supporting expression lambdas. 

I was able to work around this by defining a function which did what I intended.  I called this function in the lambda expression and the problem was solved.

Published Monday, November 19, 2007 10:38 PM by Jared Parsons

Filed under: , ,

Comments

# MSDN Blog Postings &raquo; Lambda Unexpected Behavior @ Tuesday, November 20, 2007 12:15 AM

PingBack from http://msdnrss.thecoderblogs.com/2007/11/20/lambda-unexpected-behavior/

MSDN Blog Postings » Lambda Unexpected Behavior

New Comments to this post are disabled
Page view tracker