Link to home
Start Free TrialLog in
Avatar of 3895
3895

asked on

How to call a function when I only have the functionname as a string

This is a simplified version of my problem so please dont ask why I do
it this way!

I want to create an array of a user-defined structure. The structure
contains a control and a functionname. So something like below.

MyArray = {(control1, FunctionName1),
(control2, FunctionName2),
(control3, FunctionName3), ... }

Now I want to iterate through the array and call the related function
if the control is enabled:

for each Structure in MyArray
  if Structure.control.enabled then
      Call Structure.FunctionName   'DOES NOT WORK
  end if
next

But how do I call a function just specified by its name? Can I use Eval
or do I need to specify a reference to the function in the structure
instead of the functionname?
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 3895
3895

ASKER

Both answer is exactly what I wanted! The CallByName function seems to be the most simple. Before I split the points between you, I would like to ask the following:

A problem by calling functions this way is that I can not check for misspelled functionnames at design-time, and thus can get runtime-errors. So instead of having the functionname in my structure, could I have something like a reference to the function (maybe AddressOf functionname)? This way I would avoid runtime errors! But how do I execute the function by reference?
If you are not sure if the function name is correct then how could you possibly get a correct reference?   ;)

A function reference in .Net is called a Delegate by the way...
Avatar of 3895

ASKER

I know what my functionname is but if I type it in my structure as a string, there is a chance that I misspell. I will not discover the misspell until I get a runtime error telling me that the function does not exist. If I use a delegate the program will not compile when I try to reference a misspelled function. Isn't taht correct???
Bottom line is if you are using a STRING to store the name of the function then you will not know if it is mispelled until runtime.  You will just have to catch the exception with a Try...Catch block.
Avatar of 3895

ASKER

I found a solution to call a function by reference. This way I catch misspelled functionnames at compile-time.

Public Class Form1
    Inherits System.Windows.Forms.Form  

    Delegate Sub MethodDelegate()

    Public Structure Test
        public mControl As Control
        Public mDelegate As MethodDelegate

        Public Sub New(ByVal control As Control, ByVal methodDelagate As MethodDelegate)
            mControl = control
            mDelegate = methodDelagate
        End Sub
   End Structure

   Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim lTest as new Test(textbox1, New myMethodDelegate(AddressOf function1)) 'function1 has to exist. Otherwise compile-error!
      lTest.mDelegate.Invoke() 'This calls function1
   End sub

   Private Sub function1
      msgbox "Function call"
   End sub

End class