Home All Groups Group Topic Archive Search About
Author
13 Jan 2006 7:13 AM
Shahriar
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

Author
13 Jan 2006 8:04 AM
Cor Ligthert [MVP]
dim  x as double=Cdbl(3)+Cdbl(2)*Cdbl(5)

I hope this helps,

Cor
Author
13 Jan 2006 9:22 AM
Peter Proost
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)

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
news:OH7gWfBGGHA.2012@TK2MSFTNGP14.phx.gbl...
>
> dim  x as double=Cdbl(3)+Cdbl(2)*Cdbl(5)
>
> I hope this helps,
>
> Cor
>
>
Author
13 Jan 2006 9:43 AM
Cor Ligthert [MVP]
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(MathExpression : String) : double
{
return eval(MathExpression);
};
}
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.JScript.dll as well)
5. Use from your favourite .NET language:
        Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
        MessageBox.Show(d)
6. That's it..
///

Or

By Peter Bromberg
http://www.eggheadcafe.com/articles/20030908.asp


:-))

Cor
Author
13 Jan 2006 10:06 AM
Peter Proost
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)

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
news:O9Ic1WCGGHA.3176@TK2MSFTNGP12.phx.gbl...
> 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(MathExpression : String) : double
> {
> return eval(MathExpression);
> };
> }
> 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.JScript.dll as well)
> 5. Use from your favourite .NET language:
>         Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
>         MessageBox.Show(d)
> 6. That's it..
> ///
>
> Or
>
> By Peter Bromberg
> http://www.eggheadcafe.com/articles/20030908.asp
>
>
> :-))
>
> Cor
>
>
Author
13 Jan 2006 1:30 PM
Shahriar
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





Show quoteHide quote
"Peter Proost" wrote:

> 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]" <notmyfirstn***@planet.nl> schreef in bericht
> news:O9Ic1WCGGHA.3176@TK2MSFTNGP12.phx.gbl...
> > 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(MathExpression : String) : double
> > {
> > return eval(MathExpression);
> > };
> > }
> > 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.JScript.dll as well)
> > 5. Use from your favourite .NET language:
> >         Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
> >         MessageBox.Show(d)
> > 6. That's it..
> > ///
> >
> > Or
> >
> > By Peter Bromberg
> > http://www.eggheadcafe.com/articles/20030908.asp
> >
> >
> > :-))
> >
> > Cor
> >
> >
>
>
>
Author
13 Jan 2006 4:07 PM
Kerry Moorman
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):

http://www.devx.com/vb2themax/Tip/18773

Kerry Moorman


Show quoteHide quote
"Shahriar" wrote:

> 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:
>
> > 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]" <notmyfirstn***@planet.nl> schreef in bericht
> > news:O9Ic1WCGGHA.3176@TK2MSFTNGP12.phx.gbl...
> > > 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(MathExpression : String) : double
> > > {
> > > return eval(MathExpression);
> > > };
> > > }
> > > 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.JScript.dll as well)
> > > 5. Use from your favourite .NET language:
> > >         Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
> > >         MessageBox.Show(d)
> > > 6. That's it..
> > > ///
> > >
> > > Or
> > >
> > > By Peter Bromberg
> > > http://www.eggheadcafe.com/articles/20030908.asp
> > >
> > >
> > > :-))
> > >
> > > Cor
> > >
> > >
> >
> >
> >
Author
13 Jan 2006 4:37 PM
Cor Ligthert [MVP]
Kerry,

> As you can see, it is not going to be a simple thing in VB.Net, compared
> to
> VFP.
>
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
Author
13 Jan 2006 4:41 PM
Cor Ligthert [MVP]
doh typos


"> Yes as is to pedal on a bicycle easier on a car.
>
than in a car.
Author
16 Jan 2006 3:04 AM
Mikey
In this example, TextBox1.Text would contain your expression:
    3+2*5
The result is placed into TextBox2.text.



Imports Microsoft.JScript

Try
    Dim objResult As Object = Microsoft.JScript.Eval.JScriptEvaluate( _
        TextBox1.Text, Microsoft.JScript.Vsa.VsaEngine.CreateEngine())
    Dim t As Type = objResult.GetType()
    '
    ' 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.String"
            TextBox2.Text = DirectCast(objResult, String)
        Case "System.Int32"
            TextBox2.Text = CStr(DirectCast(objResult, Integer))
        Case "System.Int64"
            TextBox2.Text = CStr(DirectCast(objResult, Long))
        Case "System.Single"
            TextBox2.Text = CStr(DirectCast(objResult, Single))
        Case "System.Double"
            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.ToString
    End Select
Catch ex As System.Exception
    MsgBox(ex.ToString)
End Try





"Shahriar" <HelloShahr***@hotmail.com> wrote in message
news:xqIxf.32064$Tn6.23843@trnddc04...
Show quoteHide quote
> 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
>