2

With ASP.NET MVC 1.0, .NET 3.5 and C# you can easily pass a method a lambda expression that will do a "Response.Write" some content when it's executed within the method:

<% Html.SomeExtensionMethod(
    () => { <%
        <p>Some page content<p>
    %> }
) %>

The method signature is similar to this:

public void SomeExtensionMethod(this HtmlHelper helper, Action pageContent)
{
    pageContent();
}

Does anyone know how to peforma a similar call using Lambda expressions in VB.NET using the same method signature shown above?

I've tried the following in VB.NET, but it wont work:

<% Html.SomeExtensionMethod(New Action( _
    Function() %> <p>Some Content</p> <% _
    )) %>

I get an exception saying "Expression Expected."

Can anyone help me with what I'm doing wrong? How do you do this in VB.NET?

4
  • use reflector. [who doesn't hate char count limit] Commented Oct 28, 2009 at 16:57
  • Wondering - what's the use case of such a method? What exactly you are trying to achieve? Commented Oct 28, 2009 at 17:05
  • This comes in very handy with ASP.NET MVC. Commented Oct 28, 2009 at 17:12
  • This is also a very simplified use case, not the full usage scenario. Commented Oct 29, 2009 at 15:37

1 Answer 1

3

VB.NET 9.0 doesn't support multi-statement lambda expressions or anonymous methods. It also does not support lambda expression that does not return a value meaning that you cannot declare an anonymous function of type Action.

Otherwise this works but is meaningless because the lambda does nothing:

<% Html.SomeExtensionMethod(New Action(Function() "<p>Some Content</p>"))%>

Now let's take your example:

<% Html.SomeExtensionMethod(New Action( _
    Function() %> <p>Some Content</p> <% _
)) %>

Should be translated to something like this:

Html.SomeExtensionMethod(New Action(Function() Html.ViewContext.HttpContext.Response.Write("<p>Some Content</p>")))

which doesn't compile in VB.NET 9.0 because your expression doesn't return a value.

In VB.NET 10 you will be able to use the Sub keyword:

Html.SomeExtensionMethod(New Action(Sub() Html.ViewContext.HttpContext.Response.Write("<p>Some Content</p>")))

which will compile fine.

Sign up to request clarification or add additional context in comments.

7 Comments

this is not true in 3.5 and 4.0. VB has support for lambdas in the newer versions of ASP.NET
Jason, How do you do this in 3.5 or 4.0?
@Jason, I didn't say that VB.NET does not support lambda expressions. I say that currently (VB.NET 10) does not support multi-statement lambda expressions.
A little rectification to my last comment: VB.NET 10 will support multi-line lambda expression. VB.NET 9.0 doesn't.
It seems that this is one way that C# is superior to VB.NET
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.