Results 1 to 13 of 13

Thread: [RESOLVED] Evaluation problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    84

    Resolved [RESOLVED] Evaluation problem

    Hi,

    This code

    Code:
    Dim ScriptControl As New MSScriptControl.ScriptControl
    ScriptControl.Language = "javascript"
    Dim Temp As Double
    Temp = ScriptControl.Eval("5^2")
    does not work correctly.
    It evaluates to 7, while it should be 25.

    How can I fix this?

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Evaluation problem

    Remove the quote marks

    Temp = ScriptControl.Eval(5^2)


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    84

    Re: Evaluation problem

    Yeah, but I'm using a string variable (the text from a textbox), so removing the quotes is not an option,
    and Val() doesn't work for that too.
    Sorry, I should have mentioned this before.

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Evaluation problem

    >the text from a textbox

    MsgBox Text1 & " = " & sc1.Eval(Text1)

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Evaluation problem

    That give 7. That was his original problem. You can't evaluate the equation from a text box because it is text


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Evaluation problem

    Code:
    Dim ScriptControl As New MSScriptControl.ScriptControl
    ScriptControl.Language = "javascript"
    Dim Temp As Double
    
    Temp = 5 ^ 2
    ScriptControl.Eval (Temp)
    Debug.Print Temp

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Evaluation problem

    Since you cannot perform math on a text string you will need to convert each operand of the equation to a numeric value first.

    Dim A() As Double 'it doesn't need to be Double - depends on possible magnitudes

    A = Split(Text1.Text,"^")

    Temp = ScriptControl.Eval(A(0)^A(1))


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Evaluation problem

    Your main problem is that "^" is not the Exponentiation Operator in javascript (it's a bitwise XOR operation so the results you're seeing are, in fact, quite correct) . I don't believe there is a native exponentiation operator in javascript and you'd normally use the Math.pwr function. (which wont work with the MSScript control's Eval Method) Any particular reason you're using javascript as opposed to vbscript ?

    Code:
    Temp = ScriptControl.Eval("2 * 3")
    works fine

    EDIT: as does
    Code:
    MSScriptControl.Language = "vbscript"
    Dim Temp As Double
    Temp = MSScriptControl.Eval("2^5")
    Debug.Print Temp
    @JM: The Eval Method of MSScript expects a valid expression in a string as the argument.
    Last edited by Doogle; Aug 3rd, 2012 at 02:04 AM. Reason: Modified first statement

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    84

    Re: Evaluation problem

    OK, thanks, I'll try VBScript right now.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    84

    Re: Evaluation problem

    Hey, it works! Thanks!!!

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Evaluation problem

    Quote Originally Posted by Doogle View Post
    Your main problem is that "^" is not the Exponentiation Operator in javascript (it's a bitwise XOR operation so the results you're seeing are, in fact, quite correct) .
    The ^ is a valid exponentiation operator in javascript as my example in post #2 proves it.

    Quote Originally Posted by Doogle View Post
    @JM: The Eval Method of MSScript expects a valid expression in a string as the argument.
    Not sure what you are saying to me but I do know that my example in post #7 is correct for his request.

    I do agree that using vbscript is far better as the quote marks don't matter,


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Evaluation problem

    Quote Originally Posted by jmsrickland View Post
    The ^ is a valid exponentiation operator in javascript as my example in post #2 proves it.
    I'm sorry to disagree, but your code in Post#2 proves that in VB6 the circumflex is the exponentiation operator. VB will evaluate 5 ^ 2 the result of which which will be passed to the Eval Method. Thus the quotation marks do matter

    (If you're not convinced try Googling 'javascript circumflex')

    Quote Originally Posted by jmsrickland View Post
    Not sure what you are saying to me but I do know that my example in post #7 is correct for his request.
    I was trying to point out that it was not necessary to go through the split operation and implied cast to a double, and that "5 ^ 2" was perfectly acceptable as the argument for the Eval Method.
    Last edited by Doogle; Aug 4th, 2012 at 11:30 PM.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Evaluation problem

    Quote Originally Posted by jmsrickland View Post
    The ^ is a valid exponentiation operator in javascript as my example in post #2 proves it.
    I'm afraid not... the only thing you have proven is that you got the answer you were expecting, and foolishly assumed it meant that everything you did was correct.

    Getting the right result does not mean that any part of the method used was correct, or that any of your assumptions were correct.

    Simply checking the data type of the parameter to Eval (which you even quoted) disproves your method. If you want evidence of it, change your code to use a dummy function:
    Code:
    Temp = Foo(5^2) 
    
    Function Foo(bar as String) as Variant
      MsgBox bar
      Foo = bar
    End Function
    ...even if done in another language and/or compiled to an external DLL, this will never show "5^2", it will always show "25".

    Your code in post #7 may give the correct outcome, but it is rather OTT... removing ScriptControl.Eval would give the same output (but would only work for the x^y situation), using ScriptControl.Eval properly would take less code and work for most situations (including things like "(2.93^7)/3+2", and many more).

    Note that your earlier claim that "you cannot perform math on a text string" shows that you don't really understand what the ScriptControl is for: running code (including math) that is contained in a string.


    While it is good to try to help people, mis-leading them through a lack of understanding is counter-productive, which is one of the reasons I didn't reply before: I don't know enough about the ScriptControl or JavaScript to have been able to answer the initial question properly.

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