Dynamic function calls in VB
-
Has anyone out there created an application using "dynamic" function calls. By dynamic I mean that you can instanciate an ActiveX object using the CreateObject() function and then make a call to a function using another variable. Maybe the following example will illustrate my questions. Let's say there is an ActiveX DLL called TestDll, that contains a class called Test, which has a method called GetText() and it returns a String. In the following code I want to access the GetText method "dynamically". See below... ... Dim obj as Object Dim sFunctionName as String Dim sResult as String ' Create my Test object Set obj = CreateObject("TestDll.Test") ' Set the name of the function that I want to call "dyanmically" sFunctionName = "GetText" 'Now dynamically call GetText 'The following line obviously doesn't work and what I'm trying to do is make a call to obj.GetText() sResult = obj.[sFunctionName] ... If anyone has any constructive ideas, I'd be appreciated.
-
Has anyone out there created an application using "dynamic" function calls. By dynamic I mean that you can instanciate an ActiveX object using the CreateObject() function and then make a call to a function using another variable. Maybe the following example will illustrate my questions. Let's say there is an ActiveX DLL called TestDll, that contains a class called Test, which has a method called GetText() and it returns a String. In the following code I want to access the GetText method "dynamically". See below... ... Dim obj as Object Dim sFunctionName as String Dim sResult as String ' Create my Test object Set obj = CreateObject("TestDll.Test") ' Set the name of the function that I want to call "dyanmically" sFunctionName = "GetText" 'Now dynamically call GetText 'The following line obviously doesn't work and what I'm trying to do is make a call to obj.GetText() sResult = obj.[sFunctionName] ... If anyone has any constructive ideas, I'd be appreciated.
Beyond a simple
IF sFunctionName = "GetText" THEN obj.GetText ELSE IF sFunctionName = "AnotherFunc" obj.AnotherFunc ENDIF
I can't think of another way off the top of my head. Michael 'Logic, my dear Zoe, merely enables one to be wrong with authority.' - The Doctor: The Wheel in Space -
Has anyone out there created an application using "dynamic" function calls. By dynamic I mean that you can instanciate an ActiveX object using the CreateObject() function and then make a call to a function using another variable. Maybe the following example will illustrate my questions. Let's say there is an ActiveX DLL called TestDll, that contains a class called Test, which has a method called GetText() and it returns a String. In the following code I want to access the GetText method "dynamically". See below... ... Dim obj as Object Dim sFunctionName as String Dim sResult as String ' Create my Test object Set obj = CreateObject("TestDll.Test") ' Set the name of the function that I want to call "dyanmically" sFunctionName = "GetText" 'Now dynamically call GetText 'The following line obviously doesn't work and what I'm trying to do is make a call to obj.GetText() sResult = obj.[sFunctionName] ... If anyone has any constructive ideas, I'd be appreciated.
How about using the CallByName function?
-
Has anyone out there created an application using "dynamic" function calls. By dynamic I mean that you can instanciate an ActiveX object using the CreateObject() function and then make a call to a function using another variable. Maybe the following example will illustrate my questions. Let's say there is an ActiveX DLL called TestDll, that contains a class called Test, which has a method called GetText() and it returns a String. In the following code I want to access the GetText method "dynamically". See below... ... Dim obj as Object Dim sFunctionName as String Dim sResult as String ' Create my Test object Set obj = CreateObject("TestDll.Test") ' Set the name of the function that I want to call "dyanmically" sFunctionName = "GetText" 'Now dynamically call GetText 'The following line obviously doesn't work and what I'm trying to do is make a call to obj.GetText() sResult = obj.[sFunctionName] ... If anyone has any constructive ideas, I'd be appreciated.
The CallByName function would do this. But I have to ask why you would want to go through such a hassle? There is no type checking of data being passed to the function or for the type of call your making. On top of that, using this function does not support return values from functions using ByRef passing and there is also a performance penalty. RageInTheMachine9532
-
The CallByName function would do this. But I have to ask why you would want to go through such a hassle? There is no type checking of data being passed to the function or for the type of call your making. On top of that, using this function does not support return values from functions using ByRef passing and there is also a performance penalty. RageInTheMachine9532
I'm in research mode and am not yet sure if I do want to go through the hassle. However, I'm trying to develop a class that is flexible enough to read different "lookup" tables from a database into a "collection" (I don't necessarily mean the Collection class). Normally, I would structure the lookup tables so that one set of code would read them all. But in this instance I don't have that luxury. Thanks for your help.