Hi John,
Thank you for posting!
I second to Tom's opinion. Using CodeDOM to generate source code for an
expression then compile it is a possible approach. Here is the online
document link in case you're interested in this technique:
http://msdn.microsoft.com/en-us/library/650ax5cx.aspx.
If you want to use the good old Script Control anyway, you can import
msscript.ocx as a reference into your project, then use the code like the
following piece to eval the script expression:
Dim sc As New ScriptControl
sc.Language = "VBScript"
Dim expr As String = "Log(10) + 4 ^ 0.5"
Dim r As Double = CDbl(sc.Eval(expr))
Console.WriteLine("{0} = {1}", expr, r)
The result will be:
Log(10) + 4 ^ 0.5 = 4.30258509299405
However, using COM interop to make use of the Script Control also brings
potential problems. For example, you might have to compile your code
explicitly as targeting x86 CPU, otherwise, on x64 Windows your code might
fail if there is no 64bit version of Script Control.
Making use of JScript.NET as Tom mentioned is a "cleaner" approach - you
don't have to worry about COM interop issues, and yet you can have a very
similar and straight forward solution.
Looking forward, Microsoft will implement Dynamic Language Runtime (DLR)
into future versions of .NET, which could be a long term solution for your
next version of application. Here is the Wikipedia page for this topic:
http://en.wikipedia.org/wiki/Dynamic_Language_Runtime.
Please let me know if you have any further questions regarding this topic.
Regards,
Jie Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.