# VBS - How to call function in a variable

**URL:** <https://boards.straightdope.com/t/vbs-how-to-call-function-in-a-variable/131375>\
**Category:** Factual Questions\
**Created:** [October 8, 2002, 3:43pm UTC](https://boards.straightdope.com/t/vbs-how-to-call-function-in-a-variable/131375 "2002-10-08T15:43:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Servo](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Servo](https://boards.straightdope.com/u/Servo)\
**Post date:** [October 8, 2002, 3:43pm UTC](https://boards.straightdope.com/t/vbs-how-to-call-function-in-a-variable/131375/1 "2002-10-08T15:43:50Z")

</div>

My co-worker is writing a VB script and she wants to call a function that is stored in a string, but she doesn’t want to write a long switch statement.

I told her it was easy to do in Perl but she wasn’t amused. Any ideas?

This doesn’t work:

```auto

dim proc as string

proc = myFunction
Call proc

```

---

<div class="post-metadata">

**Author:** ![racekarl](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@racekarl](https://boards.straightdope.com/u/racekarl)\
**Post date:** [October 8, 2002, 3:52pm UTC](https://boards.straightdope.com/t/vbs-how-to-call-function-in-a-variable/131375/2 "2002-10-08T15:52:15Z")

</div>

You use the Eval function

Also, VBScript is not strongly typed, so you can’t

Dim proc as String

it’s just

Dim proc

so you would do something like this:

Dim proc

proc = “Call myFunction()”

Call Eval(proc)

[MSDN - Use this instead of SDMB next time…](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfcteval.asp)

---

<div class="post-metadata">

**Author:** ![Servo](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Servo](https://boards.straightdope.com/u/Servo)\
**Post date:** [October 8, 2002, 4:26pm UTC](https://boards.straightdope.com/t/vbs-how-to-call-function-in-a-variable/131375/3 "2002-10-08T16:26:28Z")

</div>

Well of course I tried MSDN, but I couldn’t come up with the right search terms. [I guess I should have just browsed the list of functions, but I didn’t think of that.]

Eval didn’t work though – we kept getting syntax errors. She had to use Execute [which was at the bottom of the Eval help page you linked to].

This worked:

```auto

dim proc

proc = "Call myFunction(arg)"
Execute proc

```

Thanks for your help.
