473,804 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Evaluating A String as Code

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.

May 16 '07 #1
7 9093
On May 16, 10:31 am, d...@davesacons ulting.com wrote:
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

May 16 '07 #2
This is what I've been working on in the meantime:

Dim str As String = "if %Sales_Volume_B ase <
%Sales_Volume_B asis Then (%Price_Base - %Price_Swing ) *
(%Sales_Volume_ Basis - %Sales_Volume_B ase) else 8 - 5 end if"

Dim Return_Value As Double

str = str.Replace("%S ales_Volume_Bas e", "9000")
str = str.Replace("%S ales_Volume_Bas is", "10200")
str = str.Replace("%P rice_Base", "6")
str = str.Replace("%P rice_Swing", "5")

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

Dim sExpression As String = str.ToUpper

Dim iPosIf As Integer = InStr(sExpressi on, "IF")
Dim iPosThen As Integer = InStr(sExpressi on, "THEN")
Dim iPosElse As Integer = InStr(sExpressi on, "ELSE")
Dim iPosEndIf As Integer = InStr(sExpressi on, "END IF")
If iPosIf 0 Then
'if there is an IF THEN ELSE statement
Dim d As Double
d = obj.Eval(str.Su bstring(iPosIf + 2, iPosThen - 5))

'determine the results of the THEN clause
If d = True Then
If iPosElse 0 Then
Return_Value = obj.Eval(str.Su bstring(iPosThe n +
4, iPosElse - iPosThen - 6))
Else
If iPosEndIf 0 Then
Return_Value = obj.Eval(str.Su bstring(iPosThe n
+ 4, iPosEndIf - iPosThen - 6))
Else
Return_Value = obj.Eval(str.Su bstring(iPosThe n
+ 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.Su bstring(iPosEls e
+ 4, iPosEndIf - iPosElse - 6))
Else
Return_Value = obj.Eval(str.Su bstring(iPosEls e
+ 4))
End If
End If
Else
Return_Value = 0
End If

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

MsgBox(Return_V alue)

May 16 '07 #3
Is this what you guys are looking for
-im too new to get into this stuff yet ...but i beleive it is what you are
looking for.

I do something simillar in another programing language - execute/build
things at runtime not compile.

http://forums.microsoft.com/MSDN/Sho...67579&SiteID=1

i seem to recall an old post from months ago someone did the same using
vbscript

Miro

<da***@davesaco nsulting.comwro te in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
This is what I've been working on in the meantime:

Dim str As String = "if %Sales_Volume_B ase <
%Sales_Volume_B asis Then (%Price_Base - %Price_Swing ) *
(%Sales_Volume_ Basis - %Sales_Volume_B ase) else 8 - 5 end if"

Dim Return_Value As Double

str = str.Replace("%S ales_Volume_Bas e", "9000")
str = str.Replace("%S ales_Volume_Bas is", "10200")
str = str.Replace("%P rice_Base", "6")
str = str.Replace("%P rice_Swing", "5")

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

Dim sExpression As String = str.ToUpper

Dim iPosIf As Integer = InStr(sExpressi on, "IF")
Dim iPosThen As Integer = InStr(sExpressi on, "THEN")
Dim iPosElse As Integer = InStr(sExpressi on, "ELSE")
Dim iPosEndIf As Integer = InStr(sExpressi on, "END IF")
If iPosIf 0 Then
'if there is an IF THEN ELSE statement
Dim d As Double
d = obj.Eval(str.Su bstring(iPosIf + 2, iPosThen - 5))

'determine the results of the THEN clause
If d = True Then
If iPosElse 0 Then
Return_Value = obj.Eval(str.Su bstring(iPosThe n +
4, iPosElse - iPosThen - 6))
Else
If iPosEndIf 0 Then
Return_Value = obj.Eval(str.Su bstring(iPosThe n
+ 4, iPosEndIf - iPosThen - 6))
Else
Return_Value = obj.Eval(str.Su bstring(iPosThe n
+ 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.Su bstring(iPosEls e
+ 4, iPosEndIf - iPosElse - 6))
Else
Return_Value = obj.Eval(str.Su bstring(iPosEls e
+ 4))
End If
End If
Else
Return_Value = 0
End If

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

MsgBox(Return_V alue)

May 16 '07 #4
da***@davesacon sulting.com wrote in news:1179325891 .246076.76020
@k79g2000hse.go oglegroups.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.
May 16 '07 #5

<da***@davesaco nsulting.comwro te in message
news:11******** *************@k 79g2000hse.goog legroups.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.

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.

--
Tom Shelton

May 17 '07 #6
On May 16, 1:54 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
d...@davesacons ulting.com wrote in news:1179325891 .246076.76020
@k79g2000hse.go oglegroups.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.
Doing this in vb.net.

May 17 '07 #7
da***@davesacon sulting.com wrote in news:1179406905 .660240.121860
@q75g2000hsh.go oglegroups.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.
So the "scripting language" is VB.NET?
May 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
2996
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during the testing of it I discovered that negative numbers were being treated as if they were > 0. I compiled the following on mingw compiler/dev c++/windows xp. If I replace string.size() for a ordinary number it behaves as expected? I notice...
4
1472
by: louise raisbeck | last post by:
Hi there, I have created a vb server function which i want to evaluate when a server control button is clicked (only!) however it is evaluating the code on load even though i havent put it into the page_load function. why is it evaluating and not waiting for the calling click event? I have made it just plain sub. not private or anything FYI and have put it in the CBPage. thanks.
4
10704
by: Wade | last post by:
Hi guys, I am building a string in code that is a mathematical formula, based on a lot of criteria specified by the user. Once the formula is complete, I want to evaluate it to get a result. For example: dim strFormula as String strFormula = "((2 + 2) * 3) / 4) - 5"
30
2498
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines (There's also a SQL Server that sits on the server, but that's besides the point here.). Both adp files have references to ADOX. I've got to check Metrix has an older version of ADOX, and react appropriately. I've written code to do this, and it...
6
1724
by: bugnthecode | last post by:
I'm writing a program to send data over the serial port. I'm using pyserial, and I'm on WindowsXP. When I use literals I can get the data accross how I want it for example: 1 2 3 4 5 6 serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r') 1=Get devices attention 2=Select channel on device 3=Rate for movement
7
2266
by: temp34k45k | last post by:
I need to evaluate two strings for their order. The strings contain Letters (A thru Z upper case only) and Numbers (0-9) and the decimal point (.). I need an order like the list that follows: ( it's just an example of the order ) 0 1
15
3140
by: sara | last post by:
I am stuck. I have a report that I use in multiple places, so I call it with varying parameters (using the Where Clause in the code). I preview the report, send it to snap, then close the preview (the user can go to the server to see the snap view). Print, snap, then close is the only way I can snap with a Where clause. If there is No data that meets the criteria, I can cancel the print,
4
3978
by: jake | last post by:
Is there a way to evaluate a string expression such as "55/8" to return 6.875? Your help is greatly appreciated. jake
6
3947
by: eureka2050 | last post by:
Hi, I am a PHP coder, recently ran into a bit of a problem trying to evaluate expressions in PHP. I have an expression which is stored in a string variable and when I try to evaluate it, it always returns a 'TRUE' Eg: $str = '6.0 >= 8.5' ;
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.