|
-
Jan 10th, 2000, 08:07 PM
#1
Thread Starter
New Member
If I read in a string from a database or any other file, and the string is the name of a function in a dll. How do I get VB to call the procedure that is the name of the string that I have read from the file.
VB does not seem to be able to call procedures from variable names, is there some way I can make the VB interpreter interpret a variable name as the name of a procedure that it should be calling?
'I am using VB6.
-
Jan 10th, 2000, 11:24 PM
#2
No, not yet. It looks like VB is going in that direction. The new CallByName function allows you to use strings for Property or Method names, but not Procedure Names. I believe the best you can do now is something like:
Code:
Select Case MyImputString
Case "abc"
Call abc
Case "xyz"
Call xyz
Case Else
MsgBox "Unexpected String"
End Select
------------------
Marty
[This message has been edited by MartinLiss (edited 01-11-2000).]
-
Jan 11th, 2000, 12:56 PM
#3
Guru
Check this out Marty:
I was looking at the documentation (all 2 paragraphs of it) and I see that the argument requires an OBJECT as it's first parameter.
Now, in order to reference the current form as an object, we have to declare the form as an object. Once we do that, then we are golden:
Just add a command button to a new form
Code:
Private Sub Command1_Click()
Dim f1 As Form1
Dim procName As String
Set f1 = New Form1
procName = "Hello"
CallByName f1, procName, VbMethod
End Sub
Public Sub Hello()
MsgBox "Hi!"
End Sub
That's it! If you want to call functions in a class module in a COM (ActiveX) DLL, you just need to declare it as an object (as you normally would have to) and pass the appropriate info to CallByName
Tom
[This message has been edited by Clunietp (edited 01-11-2000).]
-
Jan 13th, 2000, 01:39 AM
#4
Guru
Even better! Great catch Marty
-
Jan 13th, 2000, 02:00 AM
#5
Thread Starter
New Member
The problem with that is that the first argument has to be a valid object type. It would be better if the object type could be a string, which is also read from the database and that string could be evaluated as a valid object type of the same name as the string.
eg
dim my_object, my_function as string
my_object = "an_object" 'a valid user defined type
my_function = "a_function" 'a valid function on this type of object
callbyname(my_object, my_function, vbmethod)
.......
my_object cannot be interpreted properly as it is a string and my_function is not a valid function on string objects.
VB does not recognise my_object to be of type an_object like I would want.
Leaving my_object as a dim doesn't work either.
Anybody writing VB7 might want to consider putting that kind of functionality in as it would be soo handy.
Using call by name still gives the same problem only in a different form.
You would still need a case statement to decide on what type of object the method is to be called as there are many methods with many functions.
CallByName would seem to be the best that VB has to offer, It would be nice if it were better though.
Thanks for the feedback
-
Jan 13th, 2000, 12:28 PM
#6
Well since forms are objects, even this works!
Code:
Option Explicit
Private Sub Command1_Click()
Dim procName As String
procName = "Hello"
CallByName Form1, procName, VbMethod
End Sub
Public Sub Hello()
MsgBox "Hi!"
End Sub
------------------
Marty
-
Jan 14th, 2000, 02:54 AM
#7
Frenzied Member
And while a variant will work, it is signifcantly slower than any other data type...
-
Jan 14th, 2000, 12:42 PM
#8
It probably doesn't matter in this case, but did you know that when you dimension variables like you did with dim my_object, my_function as string, that while my_function becomes a String, my_object becomes a Variant?
------------------
Marty
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
|