Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Dynamic function calls in VB

Dynamic function calls in VB

Scheduled Pinned Locked Moved Visual Basic
comtutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cliff Wellman
    wrote on last edited by
    #1

    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.

    M M D 3 Replies Last reply
    0
    • C Cliff Wellman

      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.

      M Offline
      M Offline
      Michael P Butler
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C Cliff Wellman

        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.

        M Offline
        M Offline
        MS le Roux
        wrote on last edited by
        #3

        How about using the CallByName function?

        1 Reply Last reply
        0
        • C Cliff Wellman

          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.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          C 1 Reply Last reply
          0
          • D Dave Kreskowiak

            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

            C Offline
            C Offline
            Cliff Wellman
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups