- Posted by miketeye on June 29, 2010
Share on FacebookI 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.