title: What is the VB equivalent of this C# syntax, dealing with delegates?
body: <p>Is it possible to translate the following C# code into VB.NET, using VB 9.0?</p>
  
  <pre><code>delegate Stream StreamOpenerDelegate(String name);
  
  void Exec1()
  {
      WorkMethod( x =&gt; File.OpenRead(x));
  }
  
  void Exec2()
  {
      StreamOpenerDelegate opener = x =&gt; return File.OpenRead(x) ;
      WorkMethod(opener);
  }
  </code></pre>
  
  <p>Can I do something like this?:</p>
  
  <pre><code>Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream
  
  Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
      ''
  End Sub
  
  Private Sub Exec1()
      Me.WorkMethod(Function (ByVal x As String) 
          Return File.OpenRead(x)
      End Function)
  End Sub
  
  Private Sub Exec2()
      Dim opener As StreamOpenerDelegate = Function (ByVal x As String) 
          Return File.OpenRead(x)
      End Function
      Me.WorkMethod(opener)
  End Sub
  </code></pre>
  
  <p>I'm trying to write some documentation, but I don't know VB syntax.  Often I use <a href="http://www.red-gate.com/products/reflector/" rel="nofollow noreferrer">Reflector</a> to translate it, but I'm not sure it's working in this case.  I'm also not clear on where I would need line continuation characters.</p>
  
  <hr>
  
  <p><strong>ANSWER</strong><br>
  In VB9, it's not possible to have multi-line lambdas (or Sub lambdas, which I did not ask about).  In VB9, all lambdas return a value, and must be a single expression. This changes in VB10.  VB10 will allow the above syntax, but VB9 will not.  In VB9, if the logic involves multiple code lines, it must not be a lambda; you must put it into a named Function and reference it explicitly.  Like this:</p>
  
  <pre><code>Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream
  
  Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
      ''
  End Sub
  
  Function MyStreamOpener(ByVal entryName As String) As Stream
      '' possibly multiple lines here
      Return File.OpenRead(entryName)
  End Function
  
  Private Sub Exec1()
      Me.WorkMethod(AddressOf MyStreamOpener)
  End Sub
  </code></pre>
  
  <p>site: <a href="http://cs.vbcity.com/blogs/mike-mcintyre/archive/2009/10/05/visual-basic-2010-multiline-sub-lambdas.aspx" rel="nofollow noreferrer">Mike McIntyre's blog</a></p>