Call subroutine by variable value

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

    Call subroutine by variable value

    Hi,
    is it possible to call a subroutine (or a function) using variable name?

    Sub a()
    Response.write( "sub a")
    End sub

    Sub b()
    Response.write( "sub b")
    End sub

    Sub c()
    Response.write( "sub c")
    End sub

    Dim subname
    subname = "b"
    **call subname**
    'does exist something like this? or something like the javascript 'eval' ?

    Thanks..



  • McKirahan

    #2
    Re: Call subroutine by variable value

    "gabba" <donlike@thespa m.comwrote in message
    news:4506e1de$0 $998$5fc30a8@ne ws.tiscali.it.. .
    Hi,
    is it possible to call a subroutine (or a function) using variable name?
    >
    Sub a()
    Response.write( "sub a")
    End sub
    >
    Sub b()
    Response.write( "sub b")
    End sub
    >
    Sub c()
    Response.write( "sub c")
    End sub
    >
    Dim subname
    subname = "b"
    **call subname**
    'does exist something like this? or something like the javascript 'eval' ?
    "Eval" exists in VBScript and is described as:
    "Evaluates an expression and returns the result."

    "Execute" will do what you want:
    "Executes one or more specified statements."

    Option Explicit
    Const subname = "b"
    Execute subname
    Sub b()
    Response.write( "sub b")
    End sub

    Or you could use:
    Const subname = "b()"


    Comment

    • Patrice

      #3
      Re: Call subroutine by variable value

      See the VBScript "Execute" statement as a last resort...

      You could also use classes (though VBSciprt has limited support for this)
      depending on what is the overall goal.

      --
      Patrice

      "gabba" <donlike@thespa m.coma écrit dans le message de news:
      4506e1de$0$998$ 5fc30a8@news.ti scali.it...
      Hi,
      is it possible to call a subroutine (or a function) using variable name?
      >
      Sub a()
      Response.write( "sub a")
      End sub
      >
      Sub b()
      Response.write( "sub b")
      End sub
      >
      Sub c()
      Response.write( "sub c")
      End sub
      >
      Dim subname
      subname = "b"
      **call subname**
      'does exist something like this? or something like the javascript 'eval' ?
      >
      Thanks..
      >
      >
      >

      Comment

      • gabba

        #4
        Re: Call subroutine by variable value


        "McKirahan" <News@McKirahan .comha scritto nel messaggio
        >'does exist something like this? or something like the javascript 'eval'
        >?
        >
        "Eval" exists in VBScript and is described as:
        "Evaluates an expression and returns the result."
        >
        "Execute" will do what you want:
        "Executes one or more specified statements."
        Perfect, really thank you.
        this is what i'm lookink for.

        I mentioned the javascript command eval() cause it's exactly the VbScript
        Execute().

        so...now I know that javascript eval and vbscript eval are false friends ;)

        gabba


        Comment

        • Bob Barrows [MVP]

          #5
          Re: Call subroutine by variable value

          gabba wrote:
          "McKirahan" <News@McKirahan .comha scritto nel messaggio
          >
          >>'does exist something like this? or something like the javascript
          >>'eval' ?
          >>
          >"Eval" exists in VBScript and is described as:
          >"Evaluates an expression and returns the result."
          >>
          >"Execute" will do what you want:
          >"Executes one or more specified statements."
          >
          Perfect, really thank you.
          this is what i'm lookink for.
          >
          I mentioned the javascript command eval() cause it's exactly the
          VbScript Execute().
          >
          so...now I know that javascript eval and vbscript eval are false
          friends ;)
          >
          And all three are evil.

          --
          Microsoft MVP -- ASP/ASP.NET
          Please reply to the newsgroup. The email account listed in my From
          header is my spam trap, so I don't check it very often. You will get a
          quicker response by posting to the newsgroup.


          Comment

          • Justin Piper

            #6
            Re: Call subroutine by variable value

            On Tue, 12 Sep 2006 11:35:49 -0500, gabba <donlike@thespa m.comwrote:
            is it possible to call a subroutine (or a function) using variable name?
            Using Eval poses a significant security risk, so it is preferable to use
            GetRef in this situation.

            Option Explicit

            Sub a()
            WScript.Echo "sub a"
            End Sub

            Dim subname
            subname = "a"

            Dim s
            Set s = GetRef(subname)
            s

            --
            Justin Piper
            Bizco Technologies
            We help businesses optimize efficiency with information technology, audio-visual, and mobility solutions through predictable pricing and dependable support.

            Comment

            Working...