bomvoo
New Member
- Joined
- Nov 16, 2004
- Messages
- 42
I´m trying to make a program to calculate in run-time a numerical expression entered as a string. I am successful when the numerical expression contains only built-in VBA functions.
I would be grateful to get some information on how should I do to get an expression containing user defined functions to be calculated in runtime.
To clarify any doubts on the question, below are two examples. The first works, the second doesn´t, how should I do to get the second to work?
Thanks!
'**********************EXAMPLE 1 **********************
Sub working_example()
'setting input variable
X1 = 10
'string defining expression to be evaluated
f_of_x = "x^2+2*x+3"
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.Reset
sc.ExecuteStatement ("x = " & X1) 'Execute statement for x variable = X1
result = sc.Eval(f_of_x) 'Evaluate it
'this works and give result=123
End Sub
'**********************EXAMPLE 2 **********************
Sub not_working_example()
'setting input variable
X1 = 10
'string defining expression to be evaluated - This does not work
f_of_x = "operation(x)"
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.Reset
sc.ExecuteStatement ("x = " & X1) 'Execute statement for x variable = X1
result = sc.Eval(f_of_x) 'Evaluate it
'this does not work because f_of_x refers to a user defined function -operation-
'the question is how to include in a string expression a user defined function and
'get the expression calculated in run-time.
End Sub
************FUNCTION USED IN EXAMPLE 2 **********************
Function operation(x)
operation = x ^ 2 + 2 * x + 3
End Function
I would be grateful to get some information on how should I do to get an expression containing user defined functions to be calculated in runtime.
To clarify any doubts on the question, below are two examples. The first works, the second doesn´t, how should I do to get the second to work?
Thanks!
'**********************EXAMPLE 1 **********************
Sub working_example()
'setting input variable
X1 = 10
'string defining expression to be evaluated
f_of_x = "x^2+2*x+3"
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.Reset
sc.ExecuteStatement ("x = " & X1) 'Execute statement for x variable = X1
result = sc.Eval(f_of_x) 'Evaluate it
'this works and give result=123
End Sub
'**********************EXAMPLE 2 **********************
Sub not_working_example()
'setting input variable
X1 = 10
'string defining expression to be evaluated - This does not work
f_of_x = "operation(x)"
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
sc.Reset
sc.ExecuteStatement ("x = " & X1) 'Execute statement for x variable = X1
result = sc.Eval(f_of_x) 'Evaluate it
'this does not work because f_of_x refers to a user defined function -operation-
'the question is how to include in a string expression a user defined function and
'get the expression calculated in run-time.
End Sub
************FUNCTION USED IN EXAMPLE 2 **********************
Function operation(x)
operation = x ^ 2 + 2 * x + 3
End Function