Evaluating A String as Code

D

david

I have searched existing posts and have not found an answer to this
variation of an old question. I have the following string stored in a
variable

Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
else 0 End If"

How do I evaluate/execute the string as code and return the answer, in
this case: 1200?

Previous examples of eval/execute have used the MSScriptControl to
return the value of a numeric equation, but those did not have the If
Then Else syntax. MSScriptControl generates syntax errors on my
equation.
 
R

rowe_newsgroups

I have searched existing posts and have not found an answer to this
variation of an old question. I have the following string stored in a
variable

Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
else 0 End If"

How do I evaluate/execute the string as code and return the answer, in
this case: 1200?

Previous examples of eval/execute have used the MSScriptControl to
return the value of a numeric equation, but those did not have the If
Then Else syntax. MSScriptControl generates syntax errors on my
equation.

You could use regex (or instr etc) to split the string into If..Else
blocks, evaluate the expressions with the MSScriptControl, and do the
logical branching yourself. Unfortunately this is just a theory, and I
don't any sample code to show you. (Though I might write some now that
I'm intrigued).

Thanks,

Seth Rowe
 
D

david

This is what I've been working on in the meantime:

Dim str As String = "if %Sales_Volume_Base <
%Sales_Volume_Basis Then (%Price_Base - %Price_Swing ) *
(%Sales_Volume_Basis - %Sales_Volume_Base) else 8 - 5 end if"

Dim Return_Value As Double

str = str.Replace("%Sales_Volume_Base", "9000")
str = str.Replace("%Sales_Volume_Basis", "10200")
str = str.Replace("%Price_Base", "6")
str = str.Replace("%Price_Swing", "5")

Dim obj As New MSScriptControl.ScriptControl
obj.Language = "VBScript"

Dim sExpression As String = str.ToUpper

Dim iPosIf As Integer = InStr(sExpression, "IF")
Dim iPosThen As Integer = InStr(sExpression, "THEN")
Dim iPosElse As Integer = InStr(sExpression, "ELSE")
Dim iPosEndIf As Integer = InStr(sExpression, "END IF")


If iPosIf > 0 Then
'if there is an IF THEN ELSE statement
Dim d As Double
d = obj.Eval(str.Substring(iPosIf + 2, iPosThen - 5))

'determine the results of the THEN clause
If d = True Then
If iPosElse > 0 Then
Return_Value = obj.Eval(str.Substring(iPosThen +
4, iPosElse - iPosThen - 6))
Else
If iPosEndIf > 0 Then
Return_Value = obj.Eval(str.Substring(iPosThen
+ 4, iPosEndIf - iPosThen - 6))
Else
Return_Value = obj.Eval(str.Substring(iPosThen
+ 4))
End If
End If
End If

'if there is an ELSE clause, find the results
If iPosElse > 0 Then
If d = False Then
If iPosEndIf > 0 Then
Return_Value = obj.Eval(str.Substring(iPosElse
+ 4, iPosEndIf - iPosElse - 6))
Else
Return_Value = obj.Eval(str.Substring(iPosElse
+ 4))
End If
End If
Else
Return_Value = 0
End If

Else
'if there is no IF THEN ELSE
Return_Value = obj.Eval(sExpression)
End If

MsgBox(Return_Value)
 
G

Guest

(e-mail address removed) wrote in @k79g2000hse.googlegroups.com:
I have searched existing posts and have not found an answer to this
variation of an old question. I have the following string stored in a
variable

Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
else 0 End If"

How do I evaluate/execute the string as code and return the answer, in
this case: 1200?

Previous examples of eval/execute have used the MSScriptControl to
return the value of a numeric equation, but those did not have the If
Then Else syntax. MSScriptControl generates syntax errors on my
equation.


Are you trying to run VB code, VBA code or something else?

..NET has the ability to dynamically compile code and execute. I've done
that before to create an application scripting engine.
 
T

Tom Shelton

I have searched existing posts and have not found an answer to this
variation of an old question. I have the following string stored in a
variable

Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
else 0 End If"

How do I evaluate/execute the string as code and return the answer, in
this case: 1200?

Previous examples of eval/execute have used the MSScriptControl to
return the value of a numeric equation, but those did not have the If
Then Else syntax. MSScriptControl generates syntax errors on my
equation.


David,

If you are using VB.NET code, then you can use CodeDOM to compile and
execute that code at runtime. Look at System.CodeDOM.
 
D

david

(e-mail address removed) wrote in @k79g2000hse.googlegroups.com:





Are you trying to run VB code, VBA code or something else?

.NET has the ability to dynamically compile code and execute. I've done
that before to create an application scripting engine.

Doing this in vb.net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top