locked
Passing a Method as a Parameter in VB.NET

    Question

  • I have a class in which I have an Async Function, to which I want to pass an Async Function from another class. When I do this, I get the error "Expression does not produce a value." The signatures in the receiving and sending classes are:
    <Extension()> Public Async Function DrawPathGeometry(pg As PathGeometry, raisefunc As Action, lowerfunc As Action, steering As TwoMotorNavigation) As Task
    and:
    Private Async Function LowerMarkerCage() As Task
    Private Async Function RaiseMarkerCage() As Task
    Why am I receiving this error, and how can I pass the methods as parameters? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, March 17, 2014 10:02 PM

Answers

  • There's nothing special about an async method here. The async method is just a function returning a Task and you create a delegate to it the same way you'd create any other delegate.

    --Rob

    Tuesday, March 18, 2014 8:51 PM
    Owner

All replies

  • Pass in the target function as a delegate. See http://msdn.microsoft.com/en-us/library/ms172879.aspx .

    You can get the address of the function to pass with the AddressOf operator.

    --Rob

    Monday, March 17, 2014 10:34 PM
    Owner
  • As you can see from my original posting, the methods I am passing are Async methods, and the page you mention did not (as far as I could see) have any information on passing Async methods. But more importantly, they are passing the Async methods between Async methods in different classes. Is there a way to do this? Did I just miss that part? Was there a link on the page on how to do that? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, March 18, 2014 1:07 AM
  • There's nothing special about an async method here. The async method is just a function returning a Task and you create a delegate to it the same way you'd create any other delegate.

    --Rob

    Tuesday, March 18, 2014 8:51 PM
    Owner
  • OK, then is there some way I can see an example? I want to see what the signatures for the following are:

    1. The method I am passing in. This will be an Async Function with return type Task.

    2. The method to which I am passing the Async Function. This will also be an Async Function. The reason I want to see the signature for this method is so that I can see what the type for the parameter which is the delegate should be.

    I also want to see what the call to delegate should look like in the receiving method. Can I see examples for these things? I still can't get it t work. Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, March 19, 2014 5:01 PM
  • See the Async docs for a primer on writing async functions.

    --Rob

    Friday, March 21, 2014 3:09 AM
    Owner
  • The Async docs give an example of writing the async function that is being passed, but I want to see an example of the method I am passing that async function to (the parameter; is the parameter type Task, Action, some other type?) and how to call the async function from within the method I am passing the async function to. Is there anywhere I can see an example of the async method (with the parameters & parameter types) the async function is being passed to and the code that would be used to call the async function from within it? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Saturday, March 22, 2014 12:33 AM