Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call a Procedure by Variable 2

Status
Not open for further replies.

sabloomer

Technical User
Aug 8, 2003
153
US
I have a form with a combo box that holds, amoung other things, the name of a procedure I want to Call.

Is there some way to Call a procedure when the procedure name is in a variable or form object? I would like to avoid a Case Statment becaue this combo box will end up holding over 100 items when done.

I could not find a Procedure Collection, I was thinking I could execute something like ...

Call ProceduresCollection("MyName")

but I don't know if that exists, or what it looks like.

Thank You,

sabloomer

 
In a theoritical basis.. because i haven't tried it:

Have a look at System.Reflection namespace and specifically the CallByName method. Also have a look at the shared CallByName method from the Microsoft.VisualBasic.Interaction. The last i have to suggest is the use of Delegates.

I know that it can be a little tricky and me not being helpful enough, but as I said, i have not "played" with it yet.

Regards
 
Just to point out: CallByName only works if the item you want to call is not in a module...

I found this one out the hard and painfull way :-(
 
cjelec,

Unfortunately I am trying to call a procedure in a module. Any suggestions?

sabloomer
 
Put your code into a class and define the methods / finctions / properties as SHARED ! This will allow you to call them without the need of initing the class.

EG. Public Function x(ByVal a As String) As Integer
should become Public Shared Function x(ByVal a As String) As Integer
 
TipGiver,

Please excuse my lack of understanding, I have been using Visual Studio for less then a week. It seems to be a big step from VBA programming in Access to VS.

I moved my code into a new class and made sure that each procedure is "public" and "shared". I am having trouble with the CallByName function. I have looked over the web trying to find an example of how to use it. What I found was an article that said use it like ...

CallByName object where the procedure is, variable containing name of the procedure, vbMethod

I have but together the following, but I am having trouble with the "object where the procedure is" portion. Do I need to indentify a specific class I have added, of just classes in general? How to make that reference?

CallByName(, "ItemAdd", CallType.Method)

Thank you for taking the time to help the newbie,

sabloomer
 
Suppose that you have a class in a separate file, called Class1:
Code:
Public Class Class1

    Private Sub ShowMsg()
        MsgBox("ShowMsg() was called !")
    End Sub

End Class
Note that you dont have to define it either as public or shared. As i said, i haven't played with it... untill now, and i figured that out !

Code in your exe:
Code:
Imports System.Reflection

  CallByName(New Class1, "ShowMsg", CallType.Method, Nothing)

NOTES:

1. You can get the result of a property or even get the returns of a function. Just 'fix' the last 2 arguments of the CallByName.

2. If the class is in your exe file, then change the 'New Class1' to 'Me'


Hope these help.
 
Rick,
I am afraid that the New is needed.
Try it yourself. It isn't working to me without the new.

This either will not work:
Code:
  Dim c As Class1 = Nothing
  CallByName(CType(c, Class1), "ShowMsg", CallType.Method, Nothing)
 
That is a great help. Thank you all for your time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top