Multiline lambda expressions Syntax in VB 10 (VS 2010)

Share on Facebook

I did not find much documentation or samples on the web about the newly introduced VB.NET 10 multiline lambda expressions so I decided to document this little nugget to save anybody the time. The C# language has supported multiline lambdas pre VS 2010, but VB users did not have the feature until the 2010 release.

There are a few places where I found discussions on the feature online and these include: vbcity and StackOverflow and also at Channel 9. Besides the multiline statements, there is also now support for subprocedures (sub) or (void functions if your roots are deep in c).

Alright, so say, you want to wire up dependency injection with structureMap for your mvc application: StructureMap uses a recommended BootStrapper approach which helps further decouple its wiring into your global.asax application start code. Most of the samples available for this online are in c# and if you try rewritting in VB you might find the compiler complaining about the lambdas used in your custom registry class below. Most of the c# samples look like below (the sample code is from Elijah Manor's blog):

public static class Bootstrapper 
{
    public static void ConfigureStructureMap()
    {
        ObjectFactory.Initialize(x => x.AddRegistry(new MyApplicationRegistry()));            
    }
}

public class MyApplicationRegistry : Registry
{
    public MyApplicationRegistry()
    {
        Scan(assemblyScanner =>
        {
            assemblyScanner.TheCallingAssembly();
            assemblyScanner.WithDefaultConventions();
        });
    }
}

For the vb 10 version of the above code, you will have to use the new void function lambda and the multiline feature as below:

Public Shared Sub ConfigureStructureMap()
   ObjectFactory.Initialize(Sub(x) x.AddRegistry(New MyApplicationRegistry()))
End Sub

Public Class MyApplicationRegistry
   Inherits Registry

        
    Public Sub New()
            
	Scan(Sub(assemblyScanner)
                     
		assemblyScanner.TheCallingAssembly()
                     
		assemblyScanner.WithDefaultConventions()
                 
   	End Sub)
        
    End Sub
    
End Class

Sure you could use a converter, but its worth documenting it here, just in case the converter is unavailable. Hope this helps you.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListkick it on DotNetKicks.comTwitThis

Comments

August 26. 2010 05:16 AM

honda ecu

How come no one is leaving related comments to what was written? Everything everyone has on here is just nonsense and garbage. Admin please do something about this!

United States honda ecu

August 27. 2010 12:59 AM

Mens Classic Loafers

am i just blind?  i was trying to find your rss feed for posts, not the comment one

United States Mens Classic Loafers

August 27. 2010 03:27 AM

Warcraft Accessories

A  Quite   nice   input .  Any time   i  read  your  website i  read  a  different   perspective .
Thanks .

United States Warcraft Accessories

Comments are closed

About Me

When not scratching my head for solutions to software challenges, I spend my time playing with my little boy - Michael Jnr.

Quotations

"Every day I remind myself that my inner and outer life are based on the labors of other men, living and dead, and that I must exert myself in order to give in the same measure as I have received and am still receiving"
Albert Einstein

Donate with PayPal - it

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2005 - 2010

Search