|
-
Sep 2nd, 2011, 07:37 PM
#1
Thread Starter
New Member
Call a Sub with Sub Name as String [Not Solved]
Is it possible to call a sub with a string.
vb Code:
Dim subname as string subname = textbox1.text (Code for calling sub, subname)
Any help?
All help is appreciated
Last edited by CrisIsRatedX; Sep 2nd, 2011 at 07:41 PM.
-
Sep 2nd, 2011, 07:42 PM
#2
Re: Call a Sub with Sub Name as String [Not Solved]
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?
-
Sep 2nd, 2011, 07:52 PM
#3
Thread Starter
New Member
Re: Call a Sub with Sub Name as String [Not Solved]
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.
-
Sep 2nd, 2011, 08:00 PM
#4
Re: Call a Sub with Sub Name as String [Not Solved]
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.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Sep 2nd, 2011, 08:23 PM
#5
Thread Starter
New Member
Re: Call a Sub with Sub Name as String [Not Solved]
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
-
Sep 2nd, 2011, 08:37 PM
#6
Re: Call a Sub with Sub Name as String [Not Solved]
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|