2012-01-02 48 views
10

Tôi muốn tạo ra một thủ tục tham số của nó cũng là một thủ tục. Có thể không?
Tôi tạo ra một số thủ tục để được sử dụng như các thông số dưới đây:Làm thế nào để vượt qua MethodName như tham số của một thủ tục trong VBNET

Private Sub Jump(xStr as string) 
    Msgbox xStr & " is jumping." 
End Sub 

Private Sub Run(xStr as string) 
    Msgbox xStr & " is jumping." 
End Sub 

thủ tục này nên gọi các thủ tục trên:

Private Sub ExecuteProcedure(?, StringParameter) '- i do not know what to put in there 
    ? ' - name of the procedure with parameter 
End Sub 

sử dụng:

ExecuteProcedure(Jump, "StringName") 
ExecuteProcedure(Run, "StringName") 

Trả lời

19

Tôi tin rằng đoạn mã sau là một ví dụ cho những gì bạn cần.

Public Delegate Sub TestDelegate(ByVal result As TestResult) 

Private Sub RunTest(ByVal testFunction As TestDelegate) 

    Dim result As New TestResult 
    result.startTime = DateTime.Now 
    testFunction(result) 
    result.endTime = DateTime.Now 

End Sub 

Private Sub MenuItemStartTests_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemStartTests.Click 

    Debug.WriteLine("Starting Tests...") 
    Debug.WriteLine("") 
    '================================== 
    ' Add Calls to Test Modules Here 

    RunTest(AddressOf Test1) 
    RunTest(AddressOf Test2) 

    '================================== 

    Debug.WriteLine("") 
    Debug.WriteLine("Tests Completed") 

End Sub 

Toàn bộ bài viết có thể được tìm thấy tại http://dotnetref.blogspot.com/2007/07/passing-function-by-reference-in-vbnet.html

Hope this helps.

+0

cảm ơn bạn! vấn đề của tôi bây giờ đã được giải quyết. –

+0

+1 vì thường là nhiệm vụ của bên thứ ba để bỏ phiếu hơn là OP và tôi là bên thứ ba. – Lion

Các vấn đề liên quan