Results 1 to 28 of 28

Thread: Expression Builder - Thinking of abandoning it in favour of the Scripting Control!

  1. #1

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    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:
    1. Dim myEval As New cls_Exp_Build
    2.  
    3. Private Sub CreateVariables()
    4. With myEval
    5.     .Add_New_Variable "strUserName", "String"
    6.     .Add_New_Variable "lngScoreFirst", "Long"
    7.     .Add_New_Variable "lngScoreSecond", "Long"
    8.     .Add_New_Variable "lngScoreThird", "Long"
    9. End With
    10. End Sub
    11.  
    12. Private Sub SetVariables()
    13. With myEval
    14.     .Set_Var_Value("strUserName") = "KayJay"
    15.     .Set_Var_Value("lngScoreFirst") = "100"
    16.     .Set_Var_Value("lngScoreSecond") = "100"
    17.     .Set_Var_Value("lngScoreThird") = "100"
    18. End With
    19. End Sub
    20.  
    21. Private Sub Command1_Click()
    22. With myEval
    23.  
    24. MsgBox "The result of the expression " & vbCrLf & vbCrLf & _
    25.          Text1.Text & vbCrLf & vbCrLf & _
    26.          "is " & .Numerical_Evaluation(Text1.Text)
    27.  
    28. MsgBox "The average score of " & _
    29.         .Get_Var_Value("strUserName") & _
    30.         " is " & _
    31.         .Numerical_Evaluation(.Get_Var_Value("lngScoreFirst") & _
    32.         " + " & _
    33.         .Get_Var_Value("lngScoreSecond") & _
    34.         " + " & _
    35.         .Get_Var_Value("lngScoreThird") & _
    36.         " / 3")
    37.  
    38. MsgBox .Get_Var_Value("strUserName") & _
    39.         "'s average score is " & _
    40.         .Numerical_Evaluation("Avrge " & .Get_Var_Value("lngScoreFirst") & _
    41.         "," & _
    42.         .Get_Var_Value("lngScoreSecond") & _
    43.         "," & _
    44.         .Get_Var_Value("lngScoreThird"))
    45.  
    46. End With
    47.  
    48. End Sub
    49.  
    50. Private Sub Form_Load()
    51. Call CreateVariables
    52. Call SetVariables
    53.  
    54. Text1.Text = "lngScoreFirst + lngScoreSecond + lngScoreThird / 3"
    55. End Sub
    Attached Files Attached Files
    Last edited by KayJay; Jan 31st, 2003 at 06:45 AM.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  2. #2

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    ~Just bumping it up the list before I sign off~

    Thanx to all those who have dl the Class. Expecting Ur feedback

    Regards

    KayJay

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  3. #3
    Addicted Member
    Join Date
    May 2002
    Posts
    230
    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.

    Everything else seems to work fine but that...
    Attached Files Attached Files

  4. #4
    Addicted Member
    Join Date
    May 2002
    Posts
    230
    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.

    So it will be

    MSGBOX varNameTxt.text & " = " & EquationResult

  5. #5

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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

  6. #6

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Tsur, I will reply in your other thread. I would like to keep this thread for the general discussion of Expression Builders

    Regards

    KayJay

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  7. #7

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    I have updated the Class Module in my first post. It is included in this post as well. Changes include

    • Incorporated evaluation of Bracketed Expressions
    • Made the operations into separate function.
    • An "Average" function is included as a sample for extensibility
    • Changed the name of the Exposed Function Name. Its now called "Eval"
    .....
    • I have added comments.


    Comments, ideas, improvements correction?

    Regards

    KayJay

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  8. #8

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Sorry. The attachment
    Attached Files Attached Files

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I still think the Scripting Control is the better way to go...... but maybe that's just me....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Techgnome: U answered my question before I could ask. (in the other thread). Thanx. Did not know that was possible with the Script control.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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.
    I'm psycedellic like that...
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Addicted Member
    Join Date
    May 2002
    Posts
    230
    I had an idea about the "("

    Say the textbox was this:

    SfFt = 4

    ( SqFt *2000 ) / 2


    What if you used an If- Then with the "("

    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…
    Last edited by TSur; Jan 29th, 2003 at 01:52 PM.

  13. #13

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    I've already done that in the Class, TSur.

    Technome: U meant psychic, right?

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    KJ: yesno.... it was meant to be a play on words....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849


    U must be in Ur 50s if Ur still into "Psycedellic". . Jus' kidding!

    Thanx for the info on the Scripting Control pal. Cheers and Good Luck

    Regards

    KayJay

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by KayJay

    U must be in Ur 50s if Ur still into "Psycedellic". . Jus' kidding!
    KayJay
    Oh God, no, please don't make me old than I really am.... I'm justnow turning 30 (next week) as it is..... still got a ways to get to 50!.....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17
    Addicted Member
    Join Date
    May 2002
    Posts
    230
    KayJay - How would I use your class in my program? Kinda new to all this and I haven't really used classes all that much?

  18. #18
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    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.

  19. #19

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Originally posted by TSur
    KayJay - How would I use your class in my program? Kinda new to all this and I haven't really used classes all that much?
    The same code I posted in the first post, should give a pointer.

    Create and instance of the Class. Then use the OBj.Eval function to return the result.
    VB Code:
    1. Dim obj As New cls_Exp_Build
    2.  
    3. 'Usage
    4. dim result as double
    5. result = obj.eval("(25 + 8) * (5 * (10 / 2))")

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  20. #20

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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

  21. #21
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    KayJay:

    Thanks for posting the class, will take an indepth look at it today.

    BTW, agree with this:

    "Find purpose, the means will follow"

    David

  22. #22
    Addicted Member
    Join Date
    May 2002
    Posts
    230
    I am just stupid cuz I can't figure this out...

    I tried to put this in with my test button but it doesn't work

    Private Sub Command111_Click()
    Dim NewVar As String
    Dim EquationResult As Double
    Dim obj As New cls_Exp_Build

    Depth = 20


    Assign_And_Calculate_Area Val(LengthTxt.Text), Val(WidthTxt.Text), Val(DepthTxt.Text)

    EquationResult = obj.Eval(FormulaTxt.Text)
    MsgBox NewVar & " = " & EquationResult


    if you have a form that you made using the class that you can upload that would be helpful so I can see how you did it...
    Last edited by TSur; Jan 30th, 2003 at 04:27 PM.

  23. #23

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Tsur:
    I've answered to Ur previous post here

    dw85745: I did not say that, Mahatma Gandhi said it

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  24. #24
    New Member
    Join Date
    Mar 2003
    Posts
    14
    Hi KayJay,

    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)

    You think this can be done?

  25. #25
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    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

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  26. #26
    New Member
    Join Date
    Mar 2003
    Posts
    14
    ?

    What do you mean by turning it to an interpreter

  27. #27
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    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

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  28. #28
    New Member
    Join Date
    Mar 2003
    Posts
    14
    You wouldn't know of any examples for this 'interpreter'?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width