Execute method from string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Execute method from string

  1. #1
    jk1121 Guest

    Execute method from string


    Is there a way (I'm guessing thru IUnknown or IDispatch) to execute a method
    based on the contents of a string variable? For example, I pass a string
    to a sub or function that contains the class to call (DLL.clsExample), and
    another string that contains the method I want to call (GetInformation).
    I know I can instantiate the object by doing a CreateObject on the class
    string, but how can I call the method that I passed in the other string variable?
    Thanks for any help.

  2. #2
    Colin McGuigan Guest

    Re: Execute method from string


    "jk1121" <jk1121@aol.com> wrote in message news:3b66e44b$1@news.devx.com...
    >
    > Is there a way (I'm guessing thru IUnknown or IDispatch) to execute a

    method
    > based on the contents of a string variable? For example, I pass a string
    > to a sub or function that contains the class to call (DLL.clsExample), and
    > another string that contains the method I want to call (GetInformation).
    > I know I can instantiate the object by doing a CreateObject on the class
    > string, but how can I call the method that I passed in the other string

    variable?
    > Thanks for any help.


    In VB6, you can use CallByName.

    Eg,

    Dim obj As Object
    Dim varResult As Variant

    Set obj = CreateObject("MyProject.MyClass")
    CallByName obj, "MyProcedure", vbMethod, Arg1, Arg2
    varResult = CallByName(obj, "MyFunction", vbMethod, Arg1)

    --
    Colin McGuigan



  3. #3
    jk1121 Guest

    Re: Execute method from string


    Thanks Colin, works perfect!

    I can't believe I've been doing this for 3 yrs and never heard of CallByName...

    "Colin McGuigan" <cmcguigan@imany.com> wrote:
    >
    >"jk1121" <jk1121@aol.com> wrote in message news:3b66e44b$1@news.devx.com...
    >>
    >> Is there a way (I'm guessing thru IUnknown or IDispatch) to execute a

    >method
    >> based on the contents of a string variable? For example, I pass a string
    >> to a sub or function that contains the class to call (DLL.clsExample),

    and
    >> another string that contains the method I want to call (GetInformation).
    >> I know I can instantiate the object by doing a CreateObject on the class
    >> string, but how can I call the method that I passed in the other string

    >variable?
    >> Thanks for any help.

    >
    >In VB6, you can use CallByName.
    >
    >Eg,
    >
    >Dim obj As Object
    >Dim varResult As Variant
    >
    >Set obj = CreateObject("MyProject.MyClass")
    >CallByName obj, "MyProcedure", vbMethod, Arg1, Arg2
    >varResult = CallByName(obj, "MyFunction", vbMethod, Arg1)
    >
    >--
    >Colin McGuigan
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center