Is it possible to call a sub with a string.
Any help?vb Code:
Dim subname as string subname = textbox1.text (Code for calling sub, subname)
All help is appreciated
Printable View
Is it possible to call a sub with a string.
Any help?vb Code:
Dim subname as string subname = textbox1.text (Code for calling sub, subname)
All help is appreciated
It is possible but it's a rare thing to have to do, and having the user type it in is something that you would pretty much never do. Can you explain, fully and clearly, exactly what you're trying to achieve and we can then advise the best way to achieve it, whether that be using a String to store a method name or something else?
It would be using a string as the method name. I was just using a textbox to show that it should be able to be any string. Just have the name of the method/sub.
vb Code:
dim subname as string subname = "sub"
Then have it use that string subname to run the method/sub.
I'm with JMC. While it's possible it is slow, complicated, and error-prone. If you would explain what you are doing instead of how you are trying to do it, we can probably suggest a faster, easier, more reliable method. There's many ways to call a method based on what a user has done, and encoding the information as strings is usually the last resort.
I am making a function that is in a different vb class library. The user needs to put in the path of a directory and the name of the method the user will be using in the code.
vb Code:
Public Shared Function getList(ByVal path As String, ByVal method As String) Dim count As Integer = System.IO.Directory.getFiles(path).Length Dim dArr() As String = System.IO.Directory.getFiles(path) Dim i As Integer For i = 0 To count - 1 (Call Method Here) Next Return 1 End Function
Why would a user of your application know the name of a method in the original source code? That is diabolical. Furthermore, you still haven't provided what I would consider a "full and clear description". As Sitten Spynne said, you are still explaining how you're trying to do it and not what you're trying to do.
If you are determined to go down this path then I will say that you should investigate Reflection. I'm not willing to go into more detail if you aren't though, because I still have serious doubts that it's not a waste of time because it's not the most appropriate option. If you are unable or unwilling to provide a proper description of the purpose of your application (NOT its implementation) then I'm afraid that I, for one, am unable or unwilling to help further.