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.


About Me

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

Quotations

"All great things are simple, and many can be expressed in single words: freedom, justice, honor, duty, mercy, hope."
Sir Winston Churchill

Donate with PayPal - it

Calendar

<<  June 2010  >>
MoTuWeThFrSaSu
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

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