Is it possible to execute a string in VB.Net (like Eval in VBScript)

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

    Is it possible to execute a string in VB.Net (like Eval in VBScript)

    I need to be able to execute a command that is defined in a string. In
    VBScript, I can do it with the Eval function. How can I do it in VB.Net?

    I need something like:

    Dim x As Integer
    Eval("x = 3")
    MsgBox x ' Should show a popup with 3 as the message

    I'm in desperate need of this function. There must be something similar in
    VB.Net.

    Thanks in advance,
    Dan


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003


  • Mike Labosh

    #2
    Re: Is it possible to execute a string in VB.Net (like Eval in VBScript)

    > I need to be able to execute a command that is defined in a string. In[color=blue]
    > VBScript, I can do it with the Eval function. How can I do it in VB.Net?[/color]

    VB 3.0 used to have this. It was absolutely indispensable!! !!!

    There is no way that I have ever heard of to do this in VB.NET, but if you
    were to invent a utility object that has a method like this:

    Public Function Eval(ByVal expression As String) As Double

    and it can handle math, trig, all the operators, etc...

    I would buy it from you in a heartbeat.

    --
    Peace & happy computing,

    Mike Labosh, MCSD MCT
    Owner, vbSensei.Com
    "Escriba coda ergo sum." -- vbSensei


    Comment

    • Tom Shelton

      #3
      Re: Is it possible to execute a string in VB.Net (like Eval in VBScript)

      On 2003-12-10, Dan <dhartley@somew here.com> wrote:[color=blue]
      > I need to be able to execute a command that is defined in a string. In
      > VBScript, I can do it with the Eval function. How can I do it in VB.Net?
      >
      > I need something like:
      >
      > Dim x As Integer
      > Eval("x = 3")
      > MsgBox x ' Should show a popup with 3 as the message
      >
      > I'm in desperate need of this function. There must be something similar in
      > VB.Net.
      >
      > Thanks in advance,
      > Dan
      >
      >
      > ---
      > Outgoing mail is certified Virus Free.
      > Checked by AVG anti-virus system (http://www.grisoft.com).
      > Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003
      >
      >[/color]

      Dan,

      There is nothing in VB.NET, but there is in JScript.NET. I'm not
      exactly sure if this will work for your situation or not, but you can do
      something like this:

      1. Create a class in JScript -

      // JScript Source Code
      class EvalClass
      {
      function Evaluate(expres sion)
      {
      return eval(expression );
      }
      }

      2. Compile it to a dll (evaluator.dll) :

      jsc /target:library evaluator.js

      3. Reference the dll in your VB.NET project.

      4. Write Code:

      Dim ec As New EvalClass()
      Dim expr As String = "(10 + 6) / 8"
      Dim res As Double = CType(ec.Evalua te(expr), Double)

      Console.WriteLi ne("{0} = {1}", expr, res)

      You can also pass in bits of JScript code, and have them executed :)
      --
      Tom Shelton
      MVP [Visual Basic]

      Comment

      • Dan

        #4
        Re: Is it possible to execute a string in VB.Net (like Eval in VBScript)

        Thanks for the help.

        It almost does what I want but not quite. It works fine for normal
        expressions but runs into problems when the expressions use variables such
        as:

        Dim x, y As Integer, strExpression As String, ec As New EvalClass()
        x = 3
        y = 2
        strExpression = "x + y"
        ec.Evaluate(str Expression)

        It throws an error because the x and y variables are not in the
        EvalClass.Evalu ate function's scope.

        Any other suggestions?

        Thanks,
        Dan


        "Tom Shelton" <tom@mtogden.co m> wrote in message
        news:egtWDntvDH A.1088@tk2msftn gp13.phx.gbl...[color=blue]
        > On 2003-12-10, Dan <dhartley@somew here.com> wrote:[color=green]
        > > I need to be able to execute a command that is defined in a string. In
        > > VBScript, I can do it with the Eval function. How can I do it in[/color][/color]
        VB.Net?[color=blue][color=green]
        > >
        > > I need something like:
        > >
        > > Dim x As Integer
        > > Eval("x = 3")
        > > MsgBox x ' Should show a popup with 3 as the message
        > >
        > > I'm in desperate need of this function. There must be something similar[/color][/color]
        in[color=blue][color=green]
        > > VB.Net.
        > >
        > > Thanks in advance,
        > > Dan
        > >
        > >
        > > ---
        > > Outgoing mail is certified Virus Free.
        > > Checked by AVG anti-virus system (http://www.grisoft.com).
        > > Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003
        > >
        > >[/color]
        >
        > Dan,
        >
        > There is nothing in VB.NET, but there is in JScript.NET. I'm not
        > exactly sure if this will work for your situation or not, but you can do
        > something like this:
        >
        > 1. Create a class in JScript -
        >
        > // JScript Source Code
        > class EvalClass
        > {
        > function Evaluate(expres sion)
        > {
        > return eval(expression );
        > }
        > }
        >
        > 2. Compile it to a dll (evaluator.dll) :
        >
        > jsc /target:library evaluator.js
        >
        > 3. Reference the dll in your VB.NET project.
        >
        > 4. Write Code:
        >
        > Dim ec As New EvalClass()
        > Dim expr As String = "(10 + 6) / 8"
        > Dim res As Double = CType(ec.Evalua te(expr), Double)
        >
        > Console.WriteLi ne("{0} = {1}", expr, res)
        >
        > You can also pass in bits of JScript code, and have them executed :)
        > --
        > Tom Shelton
        > MVP [Visual Basic][/color]


        Comment

        • Tom Shelton

          #5
          Re: Is it possible to execute a string in VB.Net (like Eval in VBScript)

          On 2003-12-10, Dan <dhartley@somew here.com> wrote:[color=blue]
          > Thanks for the help.
          >
          > It almost does what I want but not quite. It works fine for normal
          > expressions but runs into problems when the expressions use variables such
          > as:
          >
          > Dim x, y As Integer, strExpression As String, ec As New EvalClass()
          > x = 3
          > y = 2
          > strExpression = "x + y"
          > ec.Evaluate(str Expression)
          >
          > It throws an error because the x and y variables are not in the
          > EvalClass.Evalu ate function's scope.
          >
          > Any other suggestions?
          >
          > Thanks,
          > Dan
          >[/color]

          Hmmm, what about:

          Dim strExpression As String
          Dim x, y As Integer

          x = 3
          y = 2

          strExpression = String.Format(" {0} + {1}", x, y)
          MessageBox.Show (ec.Evaluate(st rExpression).To String())

          You may also be able to do something using System.Reflecti on.Emit?
          --
          Tom Shelton
          MVP [Visual Basic]

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Is it possible to execute a string in VB.Net (like Eval in VBScript)

            * "Dan" <dhartley@somew here.com> scripsit:[color=blue]
            > I need to be able to execute a command that is defined in a string. In
            > VBScript, I can do it with the Eval function. How can I do it in VB.Net?
            >
            > I need something like:
            >
            > Dim x As Integer
            > Eval("x = 3")
            > MsgBox x ' Should show a popup with 3 as the message
            >
            > I'm in desperate need of this function. There must be something similar in
            > VB.Net.[/color]

            Samples:

            <http://www.codeproject .com/useritems/evaluator.asp>
            <http://www.codeproject .com/csharp/livecodedotnet. asp>

            If you have a DevX account:

            <http://www.devx.com/codemag/Article/10352/0/page/1>

            --
            Herfried K. Wagner [MVP]
            <http://www.mvps.org/dotnet>

            Comment

            • One Handed Man [ OHM# ]

              #7
              Re: Is it possible to execute a string in VB.Net (like Eval in VBScript)

              I probably would not have thought of that.

              Impressed !

              :-)

              OHM


              Tom Shelton wrote:[color=blue]
              > On 2003-12-10, Dan <dhartley@somew here.com> wrote:[color=green]
              >> I need to be able to execute a command that is defined in a string.
              >> In VBScript, I can do it with the Eval function. How can I do it in
              >> VB.Net?
              >>
              >> I need something like:
              >>
              >> Dim x As Integer
              >> Eval("x = 3")
              >> MsgBox x ' Should show a popup with 3 as the message
              >>
              >> I'm in desperate need of this function. There must be something
              >> similar in VB.Net.
              >>
              >> Thanks in advance,
              >> Dan
              >>
              >>
              >> ---
              >> Outgoing mail is certified Virus Free.
              >> Checked by AVG anti-virus system (http://www.grisoft.com).
              >> Version: 6.0.548 / Virus Database: 341 - Release Date: 12/5/2003
              >>
              >>[/color]
              >
              > Dan,
              >
              > There is nothing in VB.NET, but there is in JScript.NET. I'm not
              > exactly sure if this will work for your situation or not, but you can
              > do something like this:
              >
              > 1. Create a class in JScript -
              >
              > // JScript Source Code
              > class EvalClass
              > {
              > function Evaluate(expres sion)
              > {
              > return eval(expression );
              > }
              > }
              >
              > 2. Compile it to a dll (evaluator.dll) :
              >
              > jsc /target:library evaluator.js
              >
              > 3. Reference the dll in your VB.NET project.
              >
              > 4. Write Code:
              >
              > Dim ec As New EvalClass()
              > Dim expr As String = "(10 + 6) / 8"
              > Dim res As Double = CType(ec.Evalua te(expr), Double)
              >
              > Console.WriteLi ne("{0} = {1}", expr, res)
              >
              > You can also pass in bits of JScript code, and have them executed :)[/color]

              Regards - OHM# OneHandedMan@BT Internet.com


              Comment

              Working...