-
Aug 2nd, 2012, 12:38 PM
#1
Thread Starter
Lively Member
[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?
-
Aug 2nd, 2012, 01:44 PM
#2
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.
-
Aug 2nd, 2012, 04:15 PM
#3
Thread Starter
Lively Member
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.
-
Aug 2nd, 2012, 05:35 PM
#4
Re: Evaluation problem
>the text from a textbox
MsgBox Text1 & " = " & sc1.Eval(Text1)
-
Aug 2nd, 2012, 05:40 PM
#5
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.
-
Aug 2nd, 2012, 10:49 PM
#6
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
-
Aug 3rd, 2012, 12:35 AM
#7
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.
-
Aug 3rd, 2012, 01:26 AM
#8
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
-
Aug 3rd, 2012, 03:53 AM
#9
Thread Starter
Lively Member
Re: Evaluation problem
OK, thanks, I'll try VBScript right now.
-
Aug 3rd, 2012, 03:58 AM
#10
Thread Starter
Lively Member
Re: Evaluation problem
Hey, it works! Thanks!!!
-
Aug 4th, 2012, 12:50 PM
#11
Re: Evaluation problem
Originally Posted by Doogle
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.
Originally Posted by Doogle
@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.
-
Aug 4th, 2012, 11:23 PM
#12
Re: Evaluation problem
Originally Posted by jmsrickland
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')
Originally Posted by jmsrickland
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.
-
Aug 5th, 2012, 02:51 AM
#13
Re: Evaluation problem
Originally Posted by jmsrickland
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|