Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a function run a callback in VB.net

I'm afraid I have been Googling this, but can't find an answer that I understand, or can use.

In Javascript, you can run a function and set a callback function which it calls after the first function has run:

function doThis(callBack){
    // do things
    // do things
    if(callBack){
         callBack();
    }
}

Call this by: doThis(function () { alert("done") });

So after it's finished doing things it calls an alert to tell you it's done.

But how do you do the same server-side in VB.NET?

like image 610
Jamie Hartnoll Avatar asked Jan 28 '13 11:01

Jamie Hartnoll


People also ask

How do I run a callback function?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.

Can a callback function call another function?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.

How do you implement callbacks?

To implement a callback functionCreate the managed callback function. The example declares a delegate type, called CallBack , which takes two arguments (hwnd and lparam). The first argument is a handle to the window; the second argument is application-defined. In this release, both arguments must be integers.

When callback is executed?

A callback function is a function that occurs after some event has occurred. The reference in memory to the callback function is usually passed to another function. This allows the other function to execute the callback when it has completed its duties by using the language-specific syntax for executing a function.


1 Answers

Just create a method that takes an Action delegate as parameter:

Sub DoThis(callback as Action)

    'do this
    'do that

    If Not callback Is Nothing Then
        callback()
    End If

End Sub

and you can call it like

DoThis(Sub() Console.WriteLine("via callback!"))
like image 158
sloth Avatar answered Nov 15 '22 22:11

sloth