Array of functions?

  • Thread starter Thread starter Marco Trapanese
  • Start date Start date
M

Marco Trapanese

Hello,

I'm wondering if I can define an array of functions. Why? Because I want
to implement a class where there is a main sub which is called every x
second by a timer. This main sub has to check some stuff. So I call a
function that will return true or false. Well, I don't know how many
functions I need. Their number may change on each instance of the class.

Hence, if I could create an array of such a function (let's say f0, f1,
f2 and so on) in the main sub I simply cycle through all of them.


Please, could you provide me a short example of what I'm trying to do?

Thanks very much
Marco / iw2nzm
 
Hello,

I'm wondering if I can define an array of functions. Why? Because I want
to implement a class where there is a main sub which is called every x
second by a timer. This main sub has to check some stuff. So I call a
function that will return true or false. Well, I don't know how many
functions I need. Their number may change on each instance of the class.

Hence, if I could create an array of such a function (let's say f0, f1,
f2 and so on) in the main sub I simply cycle through all of them.

Please, could you provide me a short example of what I'm trying to do?

Thanks very much
Marco / iw2nzm

If you are using VS2005, you can create a delegate type:


'define a delegate type that returns a string
Public Delegate Function MyFunctionDelegate() As String

'create an array of these delegates:
Dim funcArray(10) As MyFunctionDelegate

'Instantiate an instance of the delegate, passing in the address of a
function that matches the signature
funcArray(0) = New MyFunctionDelegate(AddressOf SomeFunction)

Private Function SomeFunction() As String
'do work here
End Function


If you're using VS2008, then there is already a generic delegate type
available:

Dim funcArray(10) As Func(Of String)
funcArray(0) = New Func(Of String)(AddressOf SomeFunction)

Private Function SomeFunction() As String
'do work here
End Function



Hope this helps,

Chris
 
Hello,

I'm wondering if I can define an array of functions. Why? Because I want
to implement a class where there is a main sub which is called every x
second by a timer. This main sub has to check some stuff. So I call a
function that will return true or false. Well, I don't know how many
functions I need. Their number may change on each instance of the class.

Hence, if I could create an array of such a function (let's say f0, f1,
f2 and so on) in the main sub I simply cycle through all of them.

Please, could you provide me a short example of what I'm trying to do?

Thanks very much
Marco / iw2nzm

It's not too hard to create a C style dispatch table (array of
function pointers).

Simply create a delegate to represent the function declaration. This
line essentially says I will have a function with no parameters to it:

Public Delegate Sub NoArgsHandler()

Write your functions which all comply to the spec. (here the spec is
that they don't have parameters or a return type)

Public Sub CodeBlock1()
End Sub

Public Sub CodeBlock2()
End Sub

Now in your main function create the array of function pointers:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'this could also be Sub Main if you don't need a form.

'set up the array
Dim tableOfFunctions(2) As NoArgsHandler
tableOfFunctions(0) = New NoArgsHandler(AddressOf Func1)
'element 0 points to function which has no arguments
tableOfFunctions(1) = New NoArgsHandler(AddressOf Func2)
tableOfFunctions(2) = New NoArgsHandler(AddressOf Func3)

'now call them all
For Each my_function As NoArgsHandler In tableOfFunctions
my_function.Invoke()
Next

End Sub


Hopefully you understand this. Here's the full code if you want it,
it's just ready to be pasted under a blank form:

Public Delegate Sub NoArgsHandler()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'set up the array
Dim tableOfFunctions(2) As NoArgsHandler
tableOfFunctions(0) = New NoArgsHandler(AddressOf Func1)
tableOfFunctions(1) = New NoArgsHandler(AddressOf Func2)
tableOfFunctions(2) = New NoArgsHandler(AddressOf Func3)

'now call them all
For Each my_function As NoArgsHandler In tableOfFunctions
my_function.Invoke()
Next

End Sub

Public Sub Func1()
MessageBox.Show("Func1")
End Sub

Public Sub Func2()
MessageBox.Show("Func2")
End Sub

Public Sub Func3()
MessageBox.Show("Func3")
End Sub

Then you can do interesting stuff, like instead of using an array you
could build a linked list of function pointers based on user input and
run them at the end. Or you could have a dictionary of function names
against their pointers etc etc. I hope this helps!

Phillip Taylor
 
Phillip Taylor ha scritto:
It's not too hard to create a C style dispatch table (array of
function pointers).
[cut]

Then you can do interesting stuff, like instead of using an array you
could build a linked list of function pointers based on user input and
run them at the end. Or you could have a dictionary of function names
against their pointers etc etc. I hope this helps!

Phillip Taylor


Phillip,

I thank you very much for the time you spent to write the answer!
That is exactly what I want and now I'm already writing code :)

Cheers!
Marco / iw2nzm
 
Chris Dunaway ha scritto:
If you're using VS2008, then there is already a generic delegate type
available:

Dim funcArray(10) As Func(Of String)
funcArray(0) = New Func(Of String)(AddressOf SomeFunction)

Private Function SomeFunction() As String
'do work here
End Function


Thanks to you too for the tips. Yes, I'm using VS2008 (Express Edition).

Marco / iw2nzm
 
Chris Dunaway ha scritto:
If you're using VS2008, then there is already a generic delegate type
available:

Dim funcArray(10) As Func(Of String)
funcArray(0) = New Func(Of String)(AddressOf SomeFunction)

Private Function SomeFunction() As String
'do work here
End Function


Yet another question: how to pass the "pointer-to-a-function" as parameter?

I want to do something like this (not working of course):

Dim funcArray(10) As Func(Of String)

Public Sub AddNewFunction(MyAddress as AddressOf SomeFunction)
funcArray(0) = New Func(Of String)(MyAddress)
End Sub

In fact, the MyAddress parameter should be the actual pointer to the
function SomeFunction.

Thanks again!
Marco / iw2nzm
 
Chris Dunaway ha scritto:




Yet another question: how to pass the "pointer-to-a-function" as parameter?

I want to do something like this (not working of course):

Dim funcArray(10) As Func(Of String)

Public Sub AddNewFunction(MyAddress as AddressOf SomeFunction)
funcArray(0) = New Func(Of String)(MyAddress)
End Sub

In fact, the MyAddress parameter should be the actual pointer to the
function SomeFunction.

Thanks again!
Marco / iw2nzm

You should be able to just read my code, but if you want to run it
aswell, create a new project and paste it in. On the form put a
textbox called txtFunctionName, a textbox called txtParam and a button
called btnRun. Make sure btnRun has is connected to the click event:

'A dictionary is a collection of key and pair values.
'each key is a unique string with the function name in. each value
is the pointer to the function

Dim dispatchTable As New Dictionary(Of String, functionPointer)

'everything which is a function pointer takes a string param and
'returns a boolean
Public Delegate Function functionPointer(ByVal s As String) As
Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'populate the dictionary with all your function names and
pointers
dispatchTable.Add("Func1", New functionPointer(AddressOf
Func1))
dispatchTable.Add("Func2", New functionPointer(AddressOf
Func2))
dispatchTable.Add("Func3", New functionPointer(AddressOf
Func3))

End Sub

'takes a string, returns a boolean.
Public Function Func1(ByVal s As String) As Boolean
MessageBox.Show("FUNCTION 1: " & s)
Return True
End Function

Public Function Func2(ByVal s As String) As Boolean
MessageBox.Show("FUNCTION 2: " & s)
Return True
End Function

Public Function Func3(ByVal s As String) As Boolean
MessageBox.Show("FUNCTION 3:" & s)
Return True
End Function

Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRun.Click

'see if the dictionary contains the function
If (dispatchTable.ContainsKey(txtFunctionName.Text)) Then

'if so invoke it, and pass in the contents of the param
textbox.
dispatchTable(txtFunctionName.Text).Invoke(txtParam.Text)
Else
MessageBox.Show("No function with this name in the
dictionary. type Func1, Func2 or Func3")
End If

End Sub
 
Phillip Taylor ha scritto:
You should be able to just read my code, but if you want to run it
aswell, create a new project and paste it in. On the form put a
textbox called txtFunctionName, a textbox called txtParam and a button
called btnRun. Make sure btnRun has is connected to the click event:


Thank you again for the example code, it's very kind of you.
Now I solved my problem :)

Marco / iw2nzm
 
Back
Top