Passing a function as a parameter RRS feed

  • Question

  • I am somewhat new to delegates and passing functions as parameters. Basically, I need to make a class which has a method that accepts 2 functions as parameters, and then executes those functions, sort of like the following:

    public sub mymethod(firstfunc as function,secondfunc as function)

    'execute firstfunc

    'execute secondfunc

    end sub

    The functions I will be passing will not have any parameters, and will not need to return any values (so they will actually be subs rather than functions). What should my code look like? Thanks.

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

    Saturday, January 4, 2014 10:58 PM


  • does this help?

    Public Sub Caller()
    		' giving function as parameter or an anonymous function
    		MethodThatCallsFunctions(AddressOf CalledByDelegate, Function() 
    End Function)
    	End Sub
    	Public Sub CalledByDelegate()
    	End Sub
    	Public Sub MethodThatCallsFunctions(function1 As Action, function2 As Action)
    		RaiseEvent function1()
    		RaiseEvent function2()
    	End Sub

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Anne Jing Wednesday, January 15, 2014 6:12 AM
    Sunday, January 5, 2014 12:35 AM