C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Evaluating String Expression - VB.Net


Posted Date: 11 Feb 2007    Resource Type: Articles    Category: .NET Framework
Author: Pandian SMember Level: Gold    
Rating: 1 out of 5Points: 10



This example shows how to evaluate mathematical expressions using code in VB .NET.

There are THREE steps to be done
1.Creating JavaScript Package
Open Notepad --> Enter the following Code .

package JPackage
{
Class JClass
{
function Evaluate(strExp : String) : String
{
return eval(strExp);
}
}
}

Save the aboce code with the name of 'JPackage.js'
Ex: C:\JPackage.js

2. Compiling the Package
Open .NET Command Prompt --> Do the following
C:\> jsc /t:library JPackage.js (Press ENTER)
Now, One .Dll file created (Ex: JPackage.dll)

3. Reference the .DLL file with our Project
In Solution Explorer --> Right Click on 'References' --> Add Reference..
In .NET Tab, Click Browse Button and Locate the Dll which you have created 'C:\JPackage.dll'.

Note
This solution require an aditional Namespace called 'Microsoft.JScript'
In Solution Explorer --> Right Click on 'References' --> Add Reference..
Select 'Microsoft.JScript' and Click 'Select' button and click 'OK'.

.Net Code

Imports JPackage

Dim jsp As New JClass()
Dim F As Double = 50.5
Dim strEval As String = "(" & F & " - 32.0)*5.0/9.0"
MsgBox(jsp.Evaluate(strEval))

Input:
Dim strEval As String = "(50.5 - 32.0)*5.0/9.0"

Result:
10.277777777777778

Input:
Dim strEval As String = "(1 < 3) & (3 < 1)"

Result:
0 (False)

Happy Coding
Pandian S




Responses

Author: Stephen M    18 Jul 2007Member Level: Bronze   Points : 0
I have a feeling this has more to do with my compiler, but both the ASP.NET v1.1 and v2.0 compilers won't compile the JScript:

c:\JPackage.js(3,7) : error JS1004: Expected ';'
c:\JPackage.js(10,1) : error JS1002: Syntax error
c:\JPackage.js(4,1) : error JS1234: Only type and package definitions are allowed inside a library

This is exactly what I need and is a perfect quick solution for what I need. So I hope you can help me determine my compilation issue!



Author: Stephen M    18 Jul 2007Member Level: Bronze   Points : 0
OK ... I figured it out ... simple as I expected

The class declaration must be "class" with a lower case 'c' ... I simply did a cut & paste and without any prior JScript knowledge it didn't jump out at me right away.

Anyway ... this is here for anyone else who needs this solution!


Author: Joe    13 Jul 2009Member Level: Bronze   Points : 2
I modified the code a little because the new vb.net requires strongly named binaries. Also this now supports advanced math including trig and exponential functions

import System.Reflection
[assembly:AssemblyKeyFile("C:\\mpkey.snk")]
[assembly:AssemblyVersion("1.0.0.1")]
package JPackage
{
class JClass
{
function Evaluate(strExp : String) : String
{
with(Math)
{
return eval(strExp);
}
}
}
}




Author: R.Jaya kumar (JK)    15 Jul 2009Member Level: Diamond   Points : 0
error comes here check this jpg file




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
VB.NET  .  Mathematical expression  .  Expression  .  Evaluate  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Scrolling DataGrid Programatically
Previous Resource: Extended TextBox Component Class - VB.Net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use