Expression Builder - Thinking of abandoning it in favour of the Scripting Control!
Ok. Here goes. Over the past few days a couple of threads had come up looking a method by which, numerical expression in a text box and/or a string variable can be evaluated at runtime, so that users may type in text in a textbox which will then be taken as an equation for evaluation.
One of them is this. I can't seem to find the other. Seems to have been deleted.
While the Script Control does have an "Eval" method, it cannot accomodate runtime variable, something like the poster in the above link was seeking.
I have attempted a crude approximation of such an Expression Builder with a function to handle creation & storage of runtime variables to be used in that builder. The class module is attached and the sample code is listed below.
Comments, ideas, corrections, please...
Especially on how to handle BODMAS in the expression.
VB Code:
Dim myEval As New cls_Exp_Build
Private Sub CreateVariables()
With myEval
.Add_New_Variable "strUserName", "String"
.Add_New_Variable "lngScoreFirst", "Long"
.Add_New_Variable "lngScoreSecond", "Long"
.Add_New_Variable "lngScoreThird", "Long"
End With
End Sub
Private Sub SetVariables()
With myEval
.Set_Var_Value("strUserName") = "KayJay"
.Set_Var_Value("lngScoreFirst") = "100"
.Set_Var_Value("lngScoreSecond") = "100"
.Set_Var_Value("lngScoreThird") = "100"
End With
End Sub
Private Sub Command1_Click()
With myEval
MsgBox "The result of the expression " & vbCrLf & vbCrLf & _
KayJAy,
Thanks for your help on this. I have been trying to figure out how it all works and I understand it for the most part. But I am having problems when I put in a "(" or ")"
I think it has something to do with the first one more then the second one... Not sure what I should do.
So if I make a formula that looks like this
a = SqFt + SqFt
since SqFt = 100000
this will = 200000
But if I put this
a = ( SqFt + SqFt )
it only = 100000
it doesn't count the first SqFt.
I used the code you gave me on the other post and added my formula creator to it. Take a look when you got time and see what I need to do.
One other thing. The user wouldn't enter in the name of the Var. on the same text line as the formula itself. The formula and the name of the Var. will be save as 2 different fields in a database. So There will be another textbox like L W and Depth that will have the name Say (varNameTxt) and when it gets tested it will use that as and not A the way it is now.
Tsur, I am yet to find a method of handling Brackets. BODMAS, i.e. Brackets-Of-Division-Multiplication-Addition-Subtraction. Any equation is generally evaluated in that order. If u solve the below, U'll understand what I am trying to say.
(((25 + 1/10th of 100) / 32) - 85) * 51 + 5 - 1
Need to work on that.
As to Ur seond point, I'll look it up.
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
Originally posted by KayJay Techgnome: U answered my question before I could ask. (in the other thread). Thanx. Did not know that was possible with the Script control.
So it will search for the first "(" then the next ")" and it will pull all the text info between the ( ) and then take what was left from the text and do the math.
So it would just take and figure this out
SqFt *2000 = 8000
and it would hold 8000 in a Var. and then add the 2 varables together for this:
8000 / 2
What else I thought was what if this was a formula: So more then one set of "()"
( SqFt * 2 - (1000 - 50 ) )
IT would have to search for “(“ but if the next one was a “(“ then it would need to keep searching tell if found the following “)” and take that out first and do that.
So it would first have to pull this
( 1000 - 50 ) and do that then it would leave
So you would be left with 950
- ( SqFt * 2 )
then it would need to be in a loop so it searches to see if there are any "(" left and if so pull them out and do the equation that is in the middle.
Then this would be 8
So it would be 8 – 950
So the answer would be -942
This was just something I thought of while I was trying to sleep. I don’t know how I would go about adding it to your code… Maybe you can do something with this or at least get a idea from mine…
Stumbled across the discussion so let me put in my two cents worth.
From my knowledge there are only two solutions to the problem:
1. Prewritten functions
- use scripting or a list box which executes a self contained function which does something specific
2. Allow user to write functions
- requires a parse routine to evaluate the function and all tokens ('e.g. "(" ) contained withing the function as well as deal with any reserved words which may conflict with the compiler to be used.
- evaluated function is then compiled and called from the
main application.
Originally posted by dw85745 Stumbled across the discussion so let me put in my two cents worth.
From my knowledge there are only two solutions to the problem:
1. Prewritten functions
- use scripting or a list box which executes a self contained function which does something specific
2. Allow user to write functions
- requires a parse routine to evaluate the function and all tokens ('e.g. "(" ) contained withing the function as well as deal with any reserved words which may conflict with the compiler to be used.
- evaluated function is then compiled and called from the
main application.
Both are precisely what the Class Module does.
1) Prewritten Functions which take arguments and return result are stored in the Class Modules.
2) A parsing routine which takes a string as its argument and returns the evaluated result of that string. The string can contain Runtime created variables as well.
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
I am certainly in love with your expression builder class. I got it through a friend because a firewall blocks me from downloading the cls-file (I guess you better zip your files to keep them available to anyone).
Now I was fooling around with the code and I was wondering if it would be possible to extend this class so it could read functions as well. I was thinking of evaluating expressions like:
If(Mid(txtSomeText; 3; 2) = "GE" Or dblSomeNumber >= 4; 3; 5)
I bet u need a fusion powered shuttle to reach my place...
Posts
963
For those who learnt prefix,infix and postfix in C or the like, things
like expression evaluator is a must for them to practise coding.
And if u want to read expressions as well, might as well turn it to
an interpreter
and KayJay, nice of u to share the codes. Will see ur codes to see
wut can I improve in my own expression evaluator.
ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
Programming is fun, but only if you're not on a tight deadline
So I consider all those working engineers sad people
I bet u need a fusion powered shuttle to reach my place...
Posts
963
Sorry, slipped my keyboard
I meant, if u want to read functions(executable codes) as well,
might as well make an interpreter instead of a math expression
evaluator.
ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
Programming is fun, but only if you're not on a tight deadline
So I consider all those working engineers sad people