VB.NET, which takes other functions as a parameter and executes them

Is there a way in vb.net to create a sub / function that takes as argument some pointer to another function and allows this new sub / function to execute the passed function?

I have 10-12 xml-rpc functions that I call on a remote server. Each of these functions has different lists of arguments (one takes 1 line, the other can take 3 lines and one int, etc.). They all return an object.

As I call them, it seems that he should be able to be better. For example, every time I call any of these functions, I want to check the return value to reduce the session and do something to try to reconnect to the remote system, etc.

Using .net 3.5

Thank!

-R

+5
source share
4 answers

You must be taken ... in Func'y town

+16
source
Public Sub DoSomething(outerFunction as Func(of T))
    ' do something

    ' call passed in function
    Dim value = outerFunction
End Sub
+4
source

After several studies, I came up with a solution:

Using the CallByName Function:

MSDN Link

This allowed me to have one function that actually performed 12 separate functions, and allowed me to have a centralized error handling system:

   Private Function RunRemoteRequest(ByVal functionName As String, ByVal service_url As String, ByVal args() As Object) As Object
    Dim retnVal As Object

    Dim success As Boolean = False
    While success = False And Me._connAttemptCount < MAX_ATTEMPTS
        Try
            retnVal = CallByName(remProxy, functionName, Method, args)
            success = True
            Me._connAttemptCount = 0
        Catch ex As Exception
            Me._connAttemptCount += 1
            If ex.Message = "Error I am looking for" Then
                Me.Login()
            Else
                log.Error("Error in RunRemoteRequest(" & functionName & ").", ex)
            End If
        End Try
    End While

    RunRemoteRequest = retnVal

End Function 

Note that you need to import Microsoft.VisualBasic.CallType into the module / class you are working on.

+1
source

All Articles