Is it possible to have a function within a function?

前端 未结 2 402
谎友^
谎友^ 2021-01-28 06:47

Is it possible to have a function within a function?

Something like this:

Public Class Form1
    Private Sub button1_Click(sender As Object, e As EventAr         


        
相关标签:
2条回答
  • 2021-01-28 07:14

    It is possible to have a function within a function, called lambda expressions.

    In your case, however, it is unclear to me how it can be useful.

    • Lambda Expressions (Visual Basic) @ MSDN
    0 讨论(0)
  • 2021-01-28 07:31

    It is not possible to have a fully fledged nested function definition in VB.NET. The language does support multi-line lambda expressions which look a lot like nested functions:

    Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
      Dim anim =
        Sub()
          Me.Refresh()
          ...
        End Sub
    End Sub
    

    There are some notable differences though:

    • Cannot have a Handles clause.
    • Cannot be Implements or Overrides.
    • The instance of the lambda is named, not the Sub definition.
    • In this case anim is actually a delegate and not a function.
    0 讨论(0)
提交回复
热议问题