Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way in vb.net to create a sub/function that will take as an argument some kind of pointer to another function, and allow this new sub/function to execute the passed function?

What I have are 10-12 xml-rpc functions I am calling against a remote server. Each of these functions has different argument lists (one takes 1 string, another might take 3 strings and one int, etc). All of them return an object.

As I am calling these, it seems like it should be able to be factored better. For instance, everytime I call any of these functions, I want to test the return value for a session drop, and do something to try and reconnect to the remote system, etc.

Using .net 3.5

Thanks!

-R

like image 672
rob Avatar asked Apr 29 '10 01:04

rob


People also ask

How do you call one Function from another Function in VB net?

By default, it is Public, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. The term, method, describes a Sub or Function procedure that is accessed from outside its defining module, class, or structure.

How do you write and use methods in VB net explain?

Syntax of Visual Basic Methods In visual basic, we can create the Methods either by using Sub or Function keywords like as shown below. If we create a method with Sub keyword that will not allow us to return any value. If you want to return any value, you need to use Function keyword to create the method.


4 Answers

You need to be taken...to Func'y town

like image 181
Pierreten Avatar answered Oct 21 '22 21:10

Pierreten


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

    ' call passed in function
    Dim value = outerFunction
End Sub
like image 25
Michael Shimmins Avatar answered Oct 21 '22 22:10

Michael Shimmins


Use Func<in T, out TResult>

like image 38
Keith Nicholas Avatar answered Oct 21 '22 21:10

Keith Nicholas


After some more research, I came up with a solution:

Using the CallByName function:

MSDN reference

This allowed me to have one function that actually ran the 12 individual functions, and enabled 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 have Imports Microsoft.VisualBasic.CallType in the module/class you are working on.

like image 40
rob Avatar answered Oct 21 '22 23:10

rob