Converting a String into a Function (VB.NET)


I have an problem. I have an string datatype like this.

Dim sFunctionString as String
sFunctionString = "Msgbox(" & sMessage & ")"

so how can i convert this string to an executable function and call it?

Thank you for your support

Joerg Bonsack
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In VB, you can't.

YOu'd need to do somthing like

If left$(sFunctionString, 6) = "Msgbox" Then
   Msgbox ...
end if

And that kind of defeats the purpose
I dont think you can in VB, definately could in ANSI C, If i can get a hold of the code perhaps I could work some conversion or other but I wouldnt hold out too much hope.

Could you not just parse the string with an if/case statement for each possibilty as below ?

Dim strTemp As String
Dim strProcedure As String
Dim strParameter As String
Dim pointer As Integer

strTemp = "msgbox(""Hello"")"
pointer = InStr(1, strTemp, "(", vbTextCompare)

strProcedure = Left(strTemp, pointer - 1)

strTemp = Right(strTemp, Len(strTemp) - pointer)
pointer = InStr(1, strTemp, ")", vbTextCompare)
strTemp = Left(strTemp, pointer - 1)

strParameter = Replace(strTemp, Chr(34), "", 1, , vbTextCompare)

If strProcedure = "msgbox" Then
    MsgBox strParameter
End If
How many different commands are you trying to place in a string like that?

If it is always msgbox then there are better ways.

If there are a limited number of commands, you may consider a case statement instead of IF THEN ELSE.

With a little more info, I may be able to offer better help.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Joerg_BonsackAuthor Commented:
That is not the problem with if then else etc.

Let me explain it again:

I have a Active Searching Routine which is descibe like so:
'The namespace is Searcher
' The Properties are: folder
'                      item(smth.)

Public function Count_Sub_Folders(nCounter as integer)
Dim sFunction_Call as string
Dim nResult as integer
sFunction_Call = "Searcher."    
      for x = 1 to nCounter
           sFunction_Call = sFunction_Call & ".Folder.Item(" & nCounter & ")"
     next nCounter
sFunction_Call = sFunction_Call & ".count()"
'Here comes the problem:
' I have a builded string like:
NResult= 'Here i need the function call'
end Function

The function must be filled dynamiclly with properties.

Thank you for your support
Try this...

On a form or in a class (cannot do in a module)

' Call string function
Call CallByName(Me, sFunctionString, vbMethod)

Example on a form:

Public Sub Test(byVal sMessage as string)

 Call MsgBox(sMessage)

End Sub

Private Sub cmdButton_Click()

Call CallByName(Me, "Test", vbMethod, "Message 2B Displayed")

End Sub
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
Joerg_Bonsack, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
PAQed - no points refunded (of 75)

E-E Admin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.