Aaron, Spurred by your need, I did write a class (Evaluator) to do it for you. Find it at:
http://adoguy.com/adonetexamples Thanks, Shawn Wildermuth [EMAIL PROTECTED] > -----Original Message----- > From: dotnet discussion [mailto:[EMAIL PROTECTED]] > On Behalf Of Aaron Fanetti > Sent: Monday, April 15, 2002 11:41 AM > To: [EMAIL PROTECTED] > Subject: Re: [DOTNET] Evaluate math expression strings? > > > Nice. I think I'll go the CodeDom route. It spurred on a > couple of ideas that should work well with my particular > problem too. Thanks to all! > > Aaron > > -----Original Message----- > From: dotnet discussion [mailto:[EMAIL PROTECTED]]On > Behalf Of Shawn Wildermuth > Sent: Monday, April 15, 2002 8:46 AM > To: [EMAIL PROTECTED] > Subject: Re: [DOTNET] Evaluate math expression strings? > > > Shouldn't this work: > > using System; > using System.CodeDom; > using System.CodeDom.Compiler; > using Microsoft.CSharp; > using System.Reflection; > > class Class1 > { > static void Main(string[] args) > { > string eval = "(10 + 3) * 5"; > Console.WriteLine("Evaluated: {0}", Evaluator(eval)); > } > > static int Evaluator(string eval) > { > ICodeCompiler comp = (new CSharpCodeProvider().CreateCompiler()); > CompilerParameters cp = new CompilerParameters(); > cp.ReferencedAssemblies.Add("system.dll"); > cp.GenerateExecutable = false; > cp.GenerateInMemory = true; > > string code = "using System; "; > code += "namespace MyTest { "; > code += " public class Evaluator { "; > code += " public int GetInt() { "; > code += " return (" + eval + "); "; > code += "} } }"; > > CompilerResults cr = comp.CompileAssemblyFromSource(cp, code); > Assembly a = cr.CompiledAssembly; > Object o = a.CreateInstance("MyTest.Evaluator"); > Type type = o.GetType(); > MethodInfo mi = type.GetMethod("GetInt"); > return (int) mi.Invoke(o, null); > } > } > > > Thanks, > > Shawn Wildermuth > [EMAIL PROTECTED] > > > -----Original Message----- > > From: dotnet discussion [mailto:[EMAIL PROTECTED]] > > On Behalf Of Shawn Wildermuth > > Sent: Monday, April 15, 2002 8:58 AM > > To: [EMAIL PROTECTED] > > Subject: Re: [DOTNET] Evaluate math expression strings? > > > > > > Can't you use the CodeDOM to do this? Wrap the whole thing > in a class > > (in the CodeDOM) and evaluate it. No? > > > > Thanks, > > > > Shawn Wildermuth > > [EMAIL PROTECTED] > > > > > -----Original Message----- > > > From: dotnet discussion [mailto:[EMAIL PROTECTED]] > > > On Behalf Of Stephens > > > Sent: Monday, April 15, 2002 8:36 AM > > > To: [EMAIL PROTECTED] > > > Subject: [DOTNET] Evaluate math expression strings? > > > > > > > > > > Does the framework have (or is someone willing to share :) > > > ) a utility > > > > class to do this? Or will I need to build my own interpreter? > > > > Something like... > > > > > > > > int result = MyMath.Evaluate( "(10 / 2) * 3" ); > > > > > > There's some VB 6 code that evaluates simple expressions at: > > > > > http://www.vb-helper.com/HowTo/expr.zip > > > > You can read messages from the DOTNET archive, unsubscribe from > > DOTNET, or subscribe to other DevelopMentor lists at > > http://discuss.develop.com. > > > > You can read messages from the > > DOTNET archive, unsubscribe from DOTNET, or subscribe to other > > DevelopMentor lists at http://discuss.develop.com. > > > > You can read messages from the DOTNET archive, unsubscribe > from DOTNET, or subscribe to other DevelopMentor lists at > http://discuss.develop.com. > > You can read messages from the > DOTNET archive, unsubscribe from DOTNET, or subscribe to > other DevelopMentor lists at http://discuss.develop.com. > You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.