Good day Gurus!
Am trying to call a Sub that has parameter from another Sub using Action but not working.
Please I have tried to solve this error but couldn’t.
I have two Sub in my BasePage in ASP.Net as shown below;
Sub Check(mySub As Action)
mySub()
End Sub
Sub TestMsg(g As String)
MsgBox(g)
End Sub
And on click event LinkButton, am trying to call TestMsg through Check as below
Private Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click
Check(AddressOf TestMsg("Call a sub from another"))
End Sub
But am getting an error message that says addressof operand must be the name of a method (without parenthesis)
Please what is the solution to this?
Thanks in advance
2
Answers
You can get around it with a trick, but it feels like it defeats the purpose
To make it work the way you want, you might make a generic overload and call that
Still, it would be easier to just call
You can use a different version of
Action
depending on the signatures of your method.Or you can loosely use a single
Action(Of Object())
to pass a varying number of arguments to a method, but you lose tight coupling. You’ll need to trust that the caller has information about the requirements ofargs
, such as in my example below, it requires an object, integer, double (or something which is castable to those). Inspired by this Q & Avb.net does have "CallByName" feature.
You can specify the sub you call by passing a string name.
So, say this code:
output: