string calculation

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

    #1

    string calculation

    Hi
    How can I achieve the following in VB.

    Lets say

    dim x as string
    x="3+2*5"

    I want to somehow convert this to a value, thus the result should be 13.
    In VFP, one could do something like this

    x="3+2*5"
    ? &x

    many thanks
    Shahriar


  • Cor Ligthert [MVP]

    #2
    Re: string calculation


    dim x as double=Cdbl(3)+ Cdbl(2)*Cdbl(5)

    I hope this helps,

    Cor


    Comment

    • Peter Proost

      #3
      Re: string calculation

      Hi Cor,

      I think you misunderstood the op, I think he wants to know how he can get
      the result of a formula contained in a string.

      Shahriar maybe this can help: http://www.devx.com/vb2themax/Tip/19438 there
      are also other methods available if you google a little you'll find them.

      Hth

      Greetz Peter


      --
      Programming today is a race between software engineers striving to build
      bigger and better idiot-proof programs, and the Universe trying to produce
      bigger and better idiots. So far, the Universe is winning. (Rich Cook)

      "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
      news:OH7gWfBGGH A.2012@TK2MSFTN GP14.phx.gbl...[color=blue]
      >
      > dim x as double=Cdbl(3)+ Cdbl(2)*Cdbl(5)
      >
      > I hope this helps,
      >
      > Cor
      >
      >[/color]


      Comment

      • Cor Ligthert [MVP]

        #4
        Re: string calculation

        Peter,
        [color=blue]
        > I think you misunderstood the op,[/color]

        Are you sure of that?

        I was waiting for an answer to be sure that he wanted something like this.

        \\\Eval by Nigel Amstrong
        1. Create a file called: DynamicMath.js
        2. Add this code to it:
        class DynamicMath
        {
        static function Eval(MathExpres sion : String) : double
        {
        return eval(MathExpres sion);
        };
        }
        3. Compile it with the command line jsc compiler: jsc /t:library
        DynamicMath.js
        4. Add a reference to DynamicMath.dll to your project (and to
        Microsoft.JScri pt.dll as well)
        5. Use from your favourite .NET language:
        Dim d As Double = DynamicMath.Eva l("2 + 3 + 4")
        MessageBox.Show (d)
        6. That's it..
        ///

        Or

        By Peter Bromberg



        :-))

        Cor


        Comment

        • Peter Proost

          #5
          Re: string calculation

          Hi Cor this is "Greetz" again ;-))

          I think we'll just have to wait for the op to see who was right :-) ;-)

          But either way he has got 3 nice examples now :-)

          Greetz Peter

          --
          Programming today is a race between software engineers striving to build
          bigger and better idiot-proof programs, and the Universe trying to produce
          bigger and better idiots. So far, the Universe is winning. (Rich Cook)

          "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
          news:O9Ic1WCGGH A.3176@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Peter,
          >[color=green]
          > > I think you misunderstood the op,[/color]
          >
          > Are you sure of that?
          >
          > I was waiting for an answer to be sure that he wanted something like this.
          >
          > \\\Eval by Nigel Amstrong
          > 1. Create a file called: DynamicMath.js
          > 2. Add this code to it:
          > class DynamicMath
          > {
          > static function Eval(MathExpres sion : String) : double
          > {
          > return eval(MathExpres sion);
          > };
          > }
          > 3. Compile it with the command line jsc compiler: jsc /t:library
          > DynamicMath.js
          > 4. Add a reference to DynamicMath.dll to your project (and to
          > Microsoft.JScri pt.dll as well)
          > 5. Use from your favourite .NET language:
          > Dim d As Double = DynamicMath.Eva l("2 + 3 + 4")
          > MessageBox.Show (d)
          > 6. That's it..
          > ///
          >
          > Or
          >
          > By Peter Bromberg
          > http://www.eggheadcafe.com/articles/20030908.asp
          >
          >
          > :-))
          >
          > Cor
          >
          >[/color]


          Comment

          • Shahriar

            #6
            Re: string calculation

            Hi -
            Peter understood my question.
            Cor - I am sorry if it was not clear

            Assume i have a string

            dim x as string
            x="Msgbox ('hello everyone')"

            I want to execute the content.

            or...
            dim x as string
            x="(2+2)*5"

            Should produce the result of 20

            As I have indicated this is a simple thing in VFP. All you is place the "&"
            in front of the variable.

            Thanks
            Shahriar





            "Peter Proost" wrote:
            [color=blue]
            > Hi Cor this is "Greetz" again ;-))
            >
            > I think we'll just have to wait for the op to see who was right :-) ;-)
            >
            > But either way he has got 3 nice examples now :-)
            >
            > Greetz Peter
            >
            > --
            > Programming today is a race between software engineers striving to build
            > bigger and better idiot-proof programs, and the Universe trying to produce
            > bigger and better idiots. So far, the Universe is winning. (Rich Cook)
            >
            > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
            > news:O9Ic1WCGGH A.3176@TK2MSFTN GP12.phx.gbl...[color=green]
            > > Peter,
            > >[color=darkred]
            > > > I think you misunderstood the op,[/color]
            > >
            > > Are you sure of that?
            > >
            > > I was waiting for an answer to be sure that he wanted something like this.
            > >
            > > \\\Eval by Nigel Amstrong
            > > 1. Create a file called: DynamicMath.js
            > > 2. Add this code to it:
            > > class DynamicMath
            > > {
            > > static function Eval(MathExpres sion : String) : double
            > > {
            > > return eval(MathExpres sion);
            > > };
            > > }
            > > 3. Compile it with the command line jsc compiler: jsc /t:library
            > > DynamicMath.js
            > > 4. Add a reference to DynamicMath.dll to your project (and to
            > > Microsoft.JScri pt.dll as well)
            > > 5. Use from your favourite .NET language:
            > > Dim d As Double = DynamicMath.Eva l("2 + 3 + 4")
            > > MessageBox.Show (d)
            > > 6. That's it..
            > > ///
            > >
            > > Or
            > >
            > > By Peter Bromberg
            > > http://www.eggheadcafe.com/articles/20030908.asp
            > >
            > >
            > > :-))
            > >
            > > Cor
            > >
            > >[/color]
            >
            >
            >[/color]

            Comment

            • Kerry Moorman

              #7
              Re: string calculation

              Shahriar,

              As you can see, it is not going to be a simple thing in VB.Net, compared to
              VFP.

              In addition to the examples that have already been given, here is an example
              using COM Interop and the Scripting control (at least I don't think this was
              one of the other examples):

              For those who may not know this, you can have an optional Parameter in SQL. It took me forever to figure it out.


              Kerry Moorman


              "Shahriar" wrote:
              [color=blue]
              > Hi -
              > Peter understood my question.
              > Cor - I am sorry if it was not clear
              >
              > Assume i have a string
              >
              > dim x as string
              > x="Msgbox ('hello everyone')"
              >
              > I want to execute the content.
              >
              > or...
              > dim x as string
              > x="(2+2)*5"
              >
              > Should produce the result of 20
              >
              > As I have indicated this is a simple thing in VFP. All you is place the "&"
              > in front of the variable.
              >
              > Thanks
              > Shahriar
              >
              >
              >
              >
              >
              > "Peter Proost" wrote:
              >[color=green]
              > > Hi Cor this is "Greetz" again ;-))
              > >
              > > I think we'll just have to wait for the op to see who was right :-) ;-)
              > >
              > > But either way he has got 3 nice examples now :-)
              > >
              > > Greetz Peter
              > >
              > > --
              > > Programming today is a race between software engineers striving to build
              > > bigger and better idiot-proof programs, and the Universe trying to produce
              > > bigger and better idiots. So far, the Universe is winning. (Rich Cook)
              > >
              > > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schreef in bericht
              > > news:O9Ic1WCGGH A.3176@TK2MSFTN GP12.phx.gbl...[color=darkred]
              > > > Peter,
              > > >
              > > > > I think you misunderstood the op,
              > > >
              > > > Are you sure of that?
              > > >
              > > > I was waiting for an answer to be sure that he wanted something like this.
              > > >
              > > > \\\Eval by Nigel Amstrong
              > > > 1. Create a file called: DynamicMath.js
              > > > 2. Add this code to it:
              > > > class DynamicMath
              > > > {
              > > > static function Eval(MathExpres sion : String) : double
              > > > {
              > > > return eval(MathExpres sion);
              > > > };
              > > > }
              > > > 3. Compile it with the command line jsc compiler: jsc /t:library
              > > > DynamicMath.js
              > > > 4. Add a reference to DynamicMath.dll to your project (and to
              > > > Microsoft.JScri pt.dll as well)
              > > > 5. Use from your favourite .NET language:
              > > > Dim d As Double = DynamicMath.Eva l("2 + 3 + 4")
              > > > MessageBox.Show (d)
              > > > 6. That's it..
              > > > ///
              > > >
              > > > Or
              > > >
              > > > By Peter Bromberg
              > > > http://www.eggheadcafe.com/articles/20030908.asp
              > > >
              > > >
              > > > :-))
              > > >
              > > > Cor
              > > >
              > > >[/color]
              > >
              > >
              > >[/color][/color]

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: string calculation

                Kerry,
                [color=blue]
                > As you can see, it is not going to be a simple thing in VB.Net, compared
                > to
                > VFP.
                >[/color]
                Yes as is to pedal on a bicycle easier on a car.

                Although I prefer a car for 200km or more, are there as well people who
                prefer a bicycle for that.

                :-)

                Cor


                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: string calculation

                  doh typos


                  "> Yes as is to pedal on a bicycle easier on a car.[color=blue]
                  >[/color]
                  than in a car.


                  Comment

                  • Mikey

                    #10
                    Re: string calculation

                    In this example, TextBox1.Text would contain your expression:
                    3+2*5
                    The result is placed into TextBox2.text.



                    Imports Microsoft.JScri pt

                    Try
                    Dim objResult As Object = Microsoft.JScri pt.Eval.JScript Evaluate( _
                    TextBox1.Text, Microsoft.JScri pt.Vsa.VsaEngin e.CreateEngine( ))
                    Dim t As Type = objResult.GetTy pe()
                    '
                    ' Normally, you would probably know in advance which Type would be
                    ' returned by your evaluated code. For this example,
                    ' we explicitly check for several base types.
                    '
                    Select Case t.ToString
                    Case "System.Str ing"
                    TextBox2.Text = DirectCast(objR esult, String)
                    Case "System.Int 32"
                    TextBox2.Text = CStr(DirectCast (objResult, Integer))
                    Case "System.Int 64"
                    TextBox2.Text = CStr(DirectCast (objResult, Long))
                    Case "System.Sin gle"
                    TextBox2.Text = CStr(DirectCast (objResult, Single))
                    Case "System.Dou ble"
                    TextBox2.Text = CStr(DirectCast (objResult, Double))
                    Case "System.Boolean "
                    TextBox2.Text = CStr(DirectCast (objResult, Boolean))
                    Case "System.Decimal "
                    TextBox2.Text = CStr(DirectCast (objResult, Decimal))
                    Case Else
                    TextBox2.Text = objResult.ToStr ing
                    End Select
                    Catch ex As System.Exceptio n
                    MsgBox(ex.ToStr ing)
                    End Try





                    "Shahriar" <HelloShahriar@ hotmail.com> wrote in message
                    news:xqIxf.3206 4$Tn6.23843@trn ddc04...[color=blue]
                    > Hi
                    > How can I achieve the following in VB.
                    >
                    > Lets say
                    >
                    > dim x as string
                    > x="3+2*5"
                    >
                    > I want to somehow convert this to a value, thus the result should be 13.
                    > In VFP, one could do something like this
                    >
                    > x="3+2*5"
                    > ? &x
                    >
                    > many thanks
                    > Shahriar
                    >[/color]


                    Comment

                    Working...