pass a function as a parameter(argument)

  • Thread starter Thread starter Rob H
  • Start date Start date
R

Rob H

How do you pass a function (or sub) as a parameter
(argument) of another function (or sub). I need help in
both VB.net and C#. Thanks.
 
It sounds like you are looking for a delegate. Can you show me an example of
what you are trying to accomplish?
 
Hi Rob,

Let me know if this helps.

'VB
Delegate Function MyFunctionDelagate(ByVal data As Short) As Integer

Class TestClass
Public Sub New(ByVal myFunc As MyFunctionDelagate)
End Sub
End Class

Public Function MyFunction(ByVal data As Short) As Integer
Return 0
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim m1 As New TestClass(New MyFunctionDelagate(AddressOf Me.MyFunction))
End Sub 'Form1_Load

//C#
public delegate int MyFunctionDelagate(short data);

class myClass
{
public myClass(MyFunctionDelagate myFunc)
{
}
}

public int MyFunction(short data)
{
return 0;
}

private void Form1_Load(object sender, System.EventArgs e)
{
myClass m1 = new myClass(new MyFunctionDelagate(this.MyFunction));
}
 
Back
Top