string calculation

  • Thread starter Thread starter Shahriar
  • Start date Start date
S

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
 
dim x as double=Cdbl(3)+Cdbl(2)*Cdbl(5)

I hope this helps,

Cor
 
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
 
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
 
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
 
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
 
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
 
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
 
doh typos


"> Yes as is to pedal on a bicycle easier on a car.than in a car.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top