开发者

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

开发者 https://www.devze.com 2023-02-03 01:35 出处:网络
How would I declare a nested function in VB.NET? For e开发者_Go百科xample, I want to do something like this:

How would I declare a nested function in VB.NET? For e开发者_Go百科xample, 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.


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


As you noted, this is not possible.

You have several options

  • have Function two be a private function within the same class, so you can call it from Function one.
  • Create a nested class or structure on the class, again private, and call methods on that.
0

精彩评论

暂无评论...
验证码 换一张
取 消