Board index » Visual Studio » loop thru list of functions?

loop thru list of functions?

Visual Studio34
Hi all,



I need to store a list of modules.functions() in an array

and then loop thru the array and call the functions. I

have not been able to get any code to work.



Here's what I need to do:



dim myArr(3) as string

myArr(0) = "MyModA.Func1()"

myArr(2) = "MyModA.Func2()"

myArr(3) = "MyModB.Func1()"

myArr(4) = "MyModB.Func2()"



dim iCnt, iRet as integer



FOR iCnt = 0 TO myArr.Length -1

iRet = myArr(0)

NEXT iCnt



I know why this doesn't work, but I

don't know how to figure out how to approach it.



Any help will be much appreciated.



Thanks,

Marc Miller


-
 

Re:loop thru list of functions?



Marc Miller wrote:

Quote
Hi all,



I need to store a list of modules.functions() in an array

and then loop thru the array and call the functions. I

have not been able to get any code to work.



Here's what I need to do:



dim myArr(3) as string

myArr(0) = "MyModA.Func1()"

myArr(2) = "MyModA.Func2()"

myArr(3) = "MyModB.Func1()"

myArr(4) = "MyModB.Func2()"



dim iCnt, iRet as integer



FOR iCnt = 0 TO myArr.Length -1

iRet = myArr(0)

NEXT iCnt



I know why this doesn't work, but I

don't know how to figure out how to approach it.



Any help will be much appreciated.



Thanks,

Marc Miller



System.Reflection should be able to list all the functions in a module.



As for calling them, i have no idea. Can Delegates be used somehow?



B.



-

Re:loop thru list of functions?

Quote
I need to store a list of modules.functions() in an array and then

loop thru the array and call the functions. I have not been able to

get any code to work.



Here's what I need to do:



dim myArr(3) as string

myArr(0) = "MyModA.Func1()"

myArr(2) = "MyModA.Func2()"

myArr(3) = "MyModB.Func1()"

myArr(4) = "MyModB.Func2()"

dim iCnt, iRet as integer



FOR iCnt = 0 TO myArr.Length -1

iRet = myArr(0)

NEXT iCnt

I know why this doesn't work, but I

don't know how to figure out how to approach it.

Any help will be much appreciated.



Thanks,

Marc Miller



Below is a sample implementation using delegates. You will need to translate

it to use your objects and methods. The delegate scheme should be more performant

as it does not need to use reflection, but rather has direct pointers to

the method calls. Also, notice that the methods added to the list are strongly

typed rather than being latebound as strings in your example. Note that if

you need to pass parameters to the methods, things start to get a bit more

tricky. This should give you enough to get started though.



Public Class MyApplication

<STAThread()>_

Public Shared Sub Main()

Dim tester As New MyDelegates

Dim methodList As New List(Of SampleDelegate)

methodList.Add(AddressOf tester.Test1)

methodList.Add(AddressOf tester.Test2)



For Each method As [Delegate] In methodList

method.Method.Invoke(tester, Nothing)

Next



Console.ReadLine()

End Sub

End Class



Public Class MyDelegates

Public Sub Test1()

Console.WriteLine("Testing 1")

End Sub

Public Sub Test2()

Console.WriteLine("Testing 2")

End Sub

End Class



Public Delegate Sub SampleDelegate()





Jim Wooley

devauthority.com/blogs/jwooley/default.aspx">devauthority.com/blogs/jwooley/default.aspx





-

Re:loop thru list of functions?

Calling a method by its name

<URL:http://dotnet.mvps.org/dotnet/faqs/?id=callbyname&lang=en" rel="nofollow" target="_blank">dotnet.mvps.org/dotnet/faqs/>





--

Terry





"Marc Miller" wrote:



Quote
Hi all,



I need to store a list of modules.functions() in an array

and then loop thru the array and call the functions. I

have not been able to get any code to work.



Here's what I need to do:



dim myArr(3) as string

myArr(0) = "MyModA.Func1()"

myArr(2) = "MyModA.Func2()"

myArr(3) = "MyModB.Func1()"

myArr(4) = "MyModB.Func2()"



dim iCnt, iRet as integer



FOR iCnt = 0 TO myArr.Length -1

iRet = myArr(0)

NEXT iCnt



I know why this doesn't work, but I

don't know how to figure out how to approach it.



Any help will be much appreciated.



Thanks,

Marc Miller







-