473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Evaluate a passed string variable

759 512MB
Hi all !
I am looking for a way to evaluate string expressions.
I know about SCRIPT control but this control can't do my job.
Basically I wish to calculate functions. Something like this:

Say I have a mathematically function: y = E(x) where E is an expression I don't know at design time. So I need to calculate y by evaluate E at run time. Something like this:

Expand|Select|Wrap|Line Numbers
  1. Sub Test
  2. Dim Y as Double, x As Double, E as String
  3.  
  4.     x = 2
  5.     E = "x + 5"
  6.     Y = Fx(x,E)
  7.     Debug.Print Y 'Should return 7
  8.  
  9.     E = "3*x+4"
  10.     Y = Fx(x,E)
  11.     Debug.Print Y 'Should return 10
  12. End Sub
  13.  
  14. Private Function Fx(x As Double, Ex As String)
  15.     Y = Value of Ex ??????
  16. End Function
Of course the functions I need to evaluate are more complicated than this so I am looking for a general procedure.

Can you help me ?
Thank you !
Oct 23 '11 #1

✓ answered by Guido Geurs

add the component "ScriptControl" to the form.
This is the code for the calculation:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.    Call Test
  3. End Sub
  4.  
  5. Sub Test()
  6. Dim Y As Double, x As Double, E As String
  7.  
  8.     x = 2
  9.     E = "x + 5"
  10.     Y = Fx(x, E)
  11.     Debug.Print Y 'Should return 7
  12.  
  13.     E = "3*x+4"
  14.     Y = Fx(x, E)
  15.     Debug.Print Y 'Should return 10
  16. End Sub
  17.  
  18. Private Function Fx(xval As Double, Ex As String) As Double
  19. Dim EVALstring  As String
  20.    EVALstring = Replace(Ex, "x", Str(xval))
  21.    Fx = ScriptControl1.Eval(EVALstring)
  22. End Function

2 2929
Guido Geurs
767 Expert 512MB
add the component "ScriptControl" to the form.
This is the code for the calculation:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.    Call Test
  3. End Sub
  4.  
  5. Sub Test()
  6. Dim Y As Double, x As Double, E As String
  7.  
  8.     x = 2
  9.     E = "x + 5"
  10.     Y = Fx(x, E)
  11.     Debug.Print Y 'Should return 7
  12.  
  13.     E = "3*x+4"
  14.     Y = Fx(x, E)
  15.     Debug.Print Y 'Should return 10
  16. End Sub
  17.  
  18. Private Function Fx(xval As Double, Ex As String) As Double
  19. Dim EVALstring  As String
  20.    EVALstring = Replace(Ex, "x", Str(xval))
  21.    Fx = ScriptControl1.Eval(EVALstring)
  22. End Function
Oct 23 '11 #2
Mihail
759 512MB
Thank you Guido !
I did not know the function "Replace".
I use a lot of extra code to replace the value of variable in Ex.

Thank you again.
Oct 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Donna Hawkins | last post by:
I want to use javascript to redirect to a URL which has been passed as a variable (in php). I have searched but cannot find any solution. I think this code is a basic redirect: <script...
5
by: MN | last post by:
Hi All, I have a string like ... String str1 = "(15*20)+12.5"; I want to evaluate this string to get the mathematical output. Ideally output will be 312.5. How can I do with the .Net?? ...
5
by: TJS | last post by:
How can I evaluate this string in vb.net " Dim submenu0 As New skmMenu.MenuItem('Home', '') "
13
by: TJS | last post by:
How can I evaluate this string in vb.net " Dim submenu0 As New skmMenu.MenuItem('Home', '') " I have tried this with skmmenu as a reference in the compile command but assembly will not...
0
by: KenRoy | last post by:
I want to be able to evaluate a string value as either True or False. So if the string was "0 Or (1 or 0)" the value True would be returned. My ultimate goal would be to have a statement similar...
2
by: parul.prasad | last post by:
How can we evaluate a string expression in if statement Linux C/C++
2
by: Daniel Reardon | last post by:
All, I'm sure that this is an old question, but I'm having problems getting a substring of a passed in variable. I can do other string manipulation with it such as concatonation, but I'm really...
1
by: mat14 | last post by:
Hi, Is it possible to evaluate a string to get a logical output? For Eg. var string1 = "true && false || true" Is there any method that i can use to evaluate this string and get an logical...
2
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string that looks like this "True AND True AND True" and...
8
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I have a custom business object of type Person. i created a page with check boxes that will post to a second page that is strongly typed with the first page and i have a function that retreives...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.