1

I am trying to write something in VB.NET based on a C# solution that I have. In the C# solution I call the function with:

somefunction(s => Console.WriteLine(s)).Wait();

And the called method is:

public static Task somefuntion(Action<string> callback);

In VB.NET I have

somefunction(Function(s) Console.WriteLine(s)).Wait()

Public Shared somefunction(ByRef callback As Action(Of string)) As Task

Any ideas what I am doing wrong?

1
  • ByRef is probably not what you want unless somefunction might provide its own callback back to the caller (which doesn't appear to be happening here). ByVal is probably what you want. The ByRef / ByVal distinction is somewhat confusing with reference types, where it refers to whether the host variable passed as an argument might be re-pointed to a different object, rather than whether a copy of the object is passed. Commented Nov 21, 2017 at 16:52

3 Answers 3

2

a. in lambda change Function(s) to Sub(s):

somefunction(Sub(s) Console.WriteLine(s)).Wait()

b. add the Function keyword to the somefunction method-signature:

Public Shared Function somefunction(ByRef callback As Action(Of String)) As Task
Sign up to request clarification or add additional context in comments.

3 Comments

passing Sub() myRoutine as a paramter for Action saved my day. Thank you very much
And for several lines? Sub(s) Console.WriteLine(s) Console.WriteLine("") ??
@Kiquenet you can write multi line, but in this case you need add End Sub. see here: Lambda Expressions (Visual Basic)
1

Check the offical doc

https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Module TestAction1
   Public Sub Main
      Dim messageTarget As Action(Of String) 

      If Environment.GetCommandLineArgs().Length > 1 Then
         messageTarget = AddressOf ShowWindowsMessage
      Else
         messageTarget = AddressOf Console.WriteLine
      End If
      messageTarget("Hello, World!")
   End Sub

   Private Sub ShowWindowsMessage(message As String)
      MsgBox(message)
   End Sub   
End Module

Comments

1

Use Sub instead of Function: somefunction(Sub(s) Console.WriteLine(s)).Wait()

Comments

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.