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

2019-04-24 15:06发布

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?

1条回答
虎瘦雄心在
2楼-- · 2019-04-24 15:34

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!"))
查看更多
登录 后发表回答