Call procedure from string value
I have a need to call a procedure in a module by a text reference. Currently I end up creating buttons for each procedure. I want to fill a listbox with references to the procedures and call the selected one from a single button click. An extensive select case is not a reasonable option.I believe you could do this in earlier versions of VB with The AddressOf operator. Unfortunately I can't find any examples for VB.Net. Can someone please point me the correct direction or post a simple example.
[503 byte] By [
joewmaki] at [2007-11-11 8:47:48]

# 1 Re: Call procedure from string value
having experienced this problem myself I was doing some research the other day, but not to much enlightenment (I'm still using a case condition as my list is not too large). But I think I would be correct to point you in the direction of using delegates combined with addressOf?
Hope you get some answers :)
# 2 Re: Call procedure from string value
Reflection is what you're looking for:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondynamicallyloadingusingtypes.asp
http://www.code-magazine.com/Article.aspx?quickid=0211161
# 3 Re: Call procedure from string value
I can get a simple delegate reference to work. I just can't figure out how to convert the string value to the procedure reference and get it to work.
This works:
Delegate Function Test_Proc(ByVal vStart As Date, ByVal vEnd As Date) As Boolean
Private Sub btnAutoRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAutoRun.Click
Delegate_Test(AddressOf DoIt, #12/14/2005#, #1/1/2006#)
End Sub
Public Function DoIt(ByVal vstart As Date, ByVal vend As Date) As Boolean
MessageBox.Show(vstart.ToShortDateString & " To " & vend.ToShortDateString, "Wohoo!")
Return True
End Function
Private Function Delegate_Test(ByVal proc As Test_Proc, ByVal vStart As Date, ByVal vend As Date) As Boolean
Dim fresult As Boolean = False
fresult = proc.Invoke(vStart, vend)
MessageBox.Show(fresult.ToString)
End Function
I just need to be able to pass:
Delegate_Test(AddressOf "DoIt", #12/14/2005#, #1/1/2006#)
and get it to work :D
# 4 Re: Call procedure from string value
Reflection is what you're looking for:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondynamicallyloadingusingtypes.asp
http://www.code-magazine.com/Article.aspx?quickid=0211161
Thanks, Phil, I'll dig into it.
# 5 Re: Call procedure from string value
joewmaki,
You might also consider using VB's CallByName function.
Kerry Moorman
# 6 Re: Call procedure from string value
Kerry: Does VB.NET support CallByName?
# 7 Re: Call procedure from string value
Phil,
Yes, at least VB.Net 2003 supports CallByName.
If it will do what you need it is probably simpler to use than reflection.
Kerry Moorman
# 8 Re: Call procedure from string value
All versions of VB.NET support CallByName.
# 9 Re: Call procedure from string value
joewmaki,
You might also consider using VB's CallByName function.
Kerry Moorman
Thanks Kerry, this looks like the simplest solution.
# 10 Re: Call procedure from string value
Yes, at least VB.Net 2003 supports CallByName.
If it will do what you need it is probably simpler to use than reflection.
Kerry Moorman
Yes, but I believe that it can only invoke a public method of a Class instance and not a standard Module.
# 11 Re: Call procedure from string value
Interesting, I did not know about CallByName. Thanks!