How to call a function where function name is a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Clepper

    How to call a function where function name is a variable

    Any way to do this? I need to call functions based on a variable. Do I
    actually have to make a case statement and call each funciton explicitly, or
    is there any way to call a function where the funciton name is a variable.

    Example:

    dim variable as string
    variable = "thisfunction() "

    call variable <---this will not work


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: How to call a function where function name is a variable

    Matt,
    The "easiest" way is to use CallByName, something like:

    Dim someobject As Object
    Dim variable As String
    variable = "thisfuncti on"
    Dim result As Object
    Dim param1 As Object
    Dim param2 As Object

    result = CallByName(some object, variable, CallType.Method , param1,
    param2)

    Where "someobject " is an instance of an object that you want to call the
    method against, can be nothing if you are calling a shared method.

    "variable" is the name of the method
    "result" is the value the method returns
    "param1" is the first parameter
    "param2" is the second parameter

    Of course if you are calling a Sub or a Property Set, then result is not
    used. Likewise param1 & param2 are only included for methods with
    parameters, you can include a variable number of parameters as parameters is
    a ParamArray argument.

    --
    Hope this helps
    Jay [MVP - Outlook]
    ..NET Application Architect, Enthusiast, & Evangelist
    T.S. Bradley - http://www.tsbradley.net


    "Matt Clepper" <MattClepper@di scussions.micro soft.com> wrote in message
    news:C32DED2D-6C79-4DC6-A44A-9EE720FD4B91@mi crosoft.com...
    | Any way to do this? I need to call functions based on a variable. Do I
    | actually have to make a case statement and call each funciton explicitly,
    or
    | is there any way to call a function where the funciton name is a variable.
    |
    | Example:
    |
    | dim variable as string
    | variable = "thisfunction() "
    |
    | call variable <---this will not work
    |
    |


    Comment

    • Flip

      #3
      Re: How to call a function where function name is a variable

      Wouldn't delegates also work in this case?



      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
      message news:%23N8FNCh5 FHA.2364@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Matt,
      > The "easiest" way is to use CallByName, something like:
      >
      > Dim someobject As Object
      > Dim variable As String
      > variable = "thisfuncti on"
      > Dim result As Object
      > Dim param1 As Object
      > Dim param2 As Object
      >
      > result = CallByName(some object, variable, CallType.Method , param1,
      > param2)
      >
      > Where "someobject " is an instance of an object that you want to call the
      > method against, can be nothing if you are calling a shared method.
      >
      > "variable" is the name of the method
      > "result" is the value the method returns
      > "param1" is the first parameter
      > "param2" is the second parameter
      >
      > Of course if you are calling a Sub or a Property Set, then result is not
      > used. Likewise param1 & param2 are only included for methods with
      > parameters, you can include a variable number of parameters as parameters
      > is
      > a ParamArray argument.
      >
      > --
      > Hope this helps
      > Jay [MVP - Outlook]
      > .NET Application Architect, Enthusiast, & Evangelist
      > T.S. Bradley - http://www.tsbradley.net
      >
      >
      > "Matt Clepper" <MattClepper@di scussions.micro soft.com> wrote in message
      > news:C32DED2D-6C79-4DC6-A44A-9EE720FD4B91@mi crosoft.com...
      > | Any way to do this? I need to call functions based on a variable. Do I
      > | actually have to make a case statement and call each funciton
      > explicitly,
      > or
      > | is there any way to call a function where the funciton name is a
      > variable.
      > |
      > | Example:
      > |
      > | dim variable as string
      > | variable = "thisfunction() "
      > |
      > | call variable <---this will not work
      > |
      > |
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: How to call a function where function name is a variable

        Flip,
        Yes delegates would work, however are they "easier" then CallByName,
        especially when you have the name of the routine in a String.

        FWIW: I suspect CallByName is a thin wrapper over delegates.

        --
        Hope this helps
        Jay [MVP - Outlook]
        ..NET Application Architect, Enthusiast, & Evangelist
        T.S. Bradley - http://www.tsbradley.net


        "Flip" <phenry_w@hotma il.com> wrote in message
        news:%23Q3qjFi5 FHA.140@TK2MSFT NGP10.phx.gbl.. .
        | Wouldn't delegates also work in this case?
        |
        |
        |
        | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
        | message news:%23N8FNCh5 FHA.2364@TK2MSF TNGP12.phx.gbl. ..
        | > Matt,
        | > The "easiest" way is to use CallByName, something like:
        | >
        | > Dim someobject As Object
        | > Dim variable As String
        | > variable = "thisfuncti on"
        | > Dim result As Object
        | > Dim param1 As Object
        | > Dim param2 As Object
        | >
        | > result = CallByName(some object, variable, CallType.Method ,
        param1,
        | > param2)
        | >
        | > Where "someobject " is an instance of an object that you want to call the
        | > method against, can be nothing if you are calling a shared method.
        | >
        | > "variable" is the name of the method
        | > "result" is the value the method returns
        | > "param1" is the first parameter
        | > "param2" is the second parameter
        | >
        | > Of course if you are calling a Sub or a Property Set, then result is not
        | > used. Likewise param1 & param2 are only included for methods with
        | > parameters, you can include a variable number of parameters as
        parameters
        | > is
        | > a ParamArray argument.
        | >
        | > --
        | > Hope this helps
        | > Jay [MVP - Outlook]
        | > .NET Application Architect, Enthusiast, & Evangelist
        | > T.S. Bradley - http://www.tsbradley.net
        | >
        | >
        | > "Matt Clepper" <MattClepper@di scussions.micro soft.com> wrote in message
        | > news:C32DED2D-6C79-4DC6-A44A-9EE720FD4B91@mi crosoft.com...
        | > | Any way to do this? I need to call functions based on a variable. Do
        I
        | > | actually have to make a case statement and call each funciton
        | > explicitly,
        | > or
        | > | is there any way to call a function where the funciton name is a
        | > variable.
        | > |
        | > | Example:
        | > |
        | > | dim variable as string
        | > | variable = "thisfunction() "
        | > |
        | > | call variable <---this will not work
        | > |
        | > |
        | >
        | >
        |
        |


        Comment

        • John A. Bailo

          #5
          Re: How to call a function where function name is a variable


          You can do this using reflection.

          MethodInfo mInfo = this.GetType(). GetMethod("myMe thod");


          Matt Clepper wrote:[color=blue]
          > Any way to do this? I need to call functions based on a variable. Do I
          > actually have to make a case statement and call each funciton explicitly, or
          > is there any way to call a function where the funciton name is a variable.
          >
          > Example:
          >
          > dim variable as string
          > variable = "thisfunction() "
          >
          > call variable <---this will not work
          >
          >[/color]

          Comment

          • John A. Bailo

            #6
            Re: How to call a function where function name is a variable


            And after that:

            mInfo.Invoke(th is, userParameters) ;


            John A. Bailo wrote:[color=blue]
            >
            > You can do this using reflection.
            >
            > MethodInfo mInfo = this.GetType(). GetMethod("myMe thod");
            >
            >
            > Matt Clepper wrote:
            >[color=green]
            >> Any way to do this? I need to call functions based on a variable. Do
            >> I actually have to make a case statement and call each funciton
            >> explicitly, or is there any way to call a function where the funciton
            >> name is a variable.
            >>
            >> Example:
            >>
            >> dim variable as string
            >> variable = "thisfunction() "
            >>
            >> call variable <---this will not work
            >>
            >>[/color][/color]

            Comment

            Working...