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.

Re: pass a function as a parameter(argument) by William

William
Sat Dec 20 11:03:38 CST 2003

It sounds like you are looking for a delegate. Can you show me an example of
what you are trying to accomplish?


"Rob H" <anonymous@discussions.microsoft.com> wrote in message
news:369e01c3c70f$d04d7580$7d02280a@phx.gbl...
> 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.



Re: pass a function as a parameter(argument) by Geoff

Geoff
Mon Dec 22 11:41:54 CST 2003

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));
}

"Rob H" <anonymous@discussions.microsoft.com> wrote in message
news:369e01c3c70f$d04d7580$7d02280a@phx.gbl...
> 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.