How do I declare a nested function in VB.NET?

Notradam Narciso picture Notradam Narciso · Jan 9, 2011 · Viewed 11.7k times · Source

How would I declare a nested function in VB.NET? For example, I want to do something like this:

Function one()
    Function two()
    End Function
End Function

However, this statement is invalid in VB.NET because of unclosed function.

Answer

Cody Gray picture Cody Gray · Jan 9, 2011

Are you asking how to write a lambda expression?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a lambda expression.

You create lambda expressions by using the Function or Sub keyword, just as you create a standard function or subroutine. However, lambda expressions are included in a statement.

For example, the following code will print "Hello World!":

Dim outputString As Action(Of String) = Sub(x As String)
                                            Console.WriteLine(x)
                                        End Sub
outputString("Hello World!")

For more examples, see here: VB.NET Lambda Expression