Home All Groups Group Topic Archive Search About
Author
29 Nov 2006 11:05 AM
Ivana
I need to compute a expression that user set as string in my project. It will
look like:

string s = "2*(a+5)/6";
and I need to compute a value of expression in string s.

Is there some class in framework that solves this problem? Is a way how
solve it to use dynamic method?

Thanks
Ivana

Author
30 Nov 2006 9:39 PM
William Sullivan
Nope.  No simple way to do this.  In fact, most programmers with a degree
have had to implement this kind of program as an excercise in both parsing
and state machines.  Its so common, I think if you do some research on Google
you'll find tons of discussion about this kind of parsing of simple
mathematical functions.

Show quote
"Ivana" wrote:

> I need to compute a expression that user set as string in my project. It will
> look like:
>
> string s = "2*(a+5)/6";
> and I need to compute a value of expression in string s.
>
> Is there some class in framework that solves this problem? Is a way how
> solve it to use dynamic method?
>
> Thanks
> Ivana
Author
8 Dec 2006 12:18 AM
Peter Ritchie [C# MVP]
There's an Eval function in JScript that will evaluate text math formulas.  I
don't think it does variables; but there's an article about how to invoke it
from C# here: http://www.odetocode.com/Code/80.aspx

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


Show quote
"Ivana" wrote:

> I need to compute a expression that user set as string in my project. It will
> look like:
>
> string s = "2*(a+5)/6";
> and I need to compute a value of expression in string s.
>
> Is there some class in framework that solves this problem? Is a way how
> solve it to use dynamic method?
>
> Thanks
> Ivana
Author
8 Dec 2006 5:54 AM
William Stacey [C# MVP]
If you will be doing a lot of this and need state vars set and read by user,
you might concider the Powershell api and setup a workspace contained by
your code (i.e. form).  Psh can do the math as well as any other script
logic you may need.

--
William Stacey [C# MVP]

Show quote
"Ivana" <Iv***@discussions.microsoft.com> wrote in message
news:05E34681-156E-41DA-B811-5159174C12E0@microsoft.com...
|I need to compute a expression that user set as string in my project. It
will
| look like:
|
| string s = "2*(a+5)/6";
| and I need to compute a value of expression in string s.
|
| Is there some class in framework that solves this problem? Is a way how
| solve it to use dynamic method?
|
| Thanks
| Ivana
Author
8 Dec 2006 6:58 PM
Richard Taylor
You could use System.CodeDom to build the code dynamically, compile it and
execute it. Or you could use CSharpCodeProvider or VBCodeProvider to compile
and execute dynamically created code. Not very efficient but it would work.

Show quote
"Ivana" <Iv***@discussions.microsoft.com> wrote in message
news:05E34681-156E-41DA-B811-5159174C12E0@microsoft.com...
>I need to compute a expression that user set as string in my project. It
>will
> look like:
>
> string s = "2*(a+5)/6";
> and I need to compute a value of expression in string s.
>
> Is there some class in framework that solves this problem? Is a way how
> solve it to use dynamic method?
>
> Thanks
> Ivana
Author
8 Dec 2006 10:18 PM
Richard Taylor
Here's an example:
using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.CSharp;

using System.CodeDom;

using System.CodeDom.Compiler;

namespace ComputeExpression

{

public interface IDynamicExpression

{

int Compute();

}

class Program

{

static void Main(string[] args)

{

CSharpCodeProvider provider = new CSharpCodeProvider();

CompilerParameters options = new CompilerParameters();

options.GenerateInMemory = true;

options.ReferencedAssemblies.Add("ComputeExpression.exe");

string source = @"namespace ComputeExpression { class Exp :
IDynamicExpression { public int Compute() { return " + args[0] + @"; } } }";

CompilerResults result = provider.CompileAssemblyFromSource(options,
source);

if(result.Errors.Count == 0)

{

IDynamicExpression de =
(IDynamicExpression)result.CompiledAssembly.CreateInstance("ComputeExpression.Exp");

Console.WriteLine(string.Format("{0} = {1}", args[0], de.Compute()));

}

else

{

foreach(CompilerError err in result.Errors)

{

Console.WriteLine("#{0} Line {1}: {2}", err.ErrorNumber, err.Line,
err.ErrorText);

}

}

}

}

}

Show quote
"Richard Taylor" <rich***@msdnnews.com> wrote in message
news:OUBMXrvGHHA.1816@TK2MSFTNGP06.phx.gbl...
> You could use System.CodeDom to build the code dynamically, compile it and
> execute it. Or you could use CSharpCodeProvider or VBCodeProvider to
> compile and execute dynamically created code. Not very efficient but it
> would work.
>
> "Ivana" <Iv***@discussions.microsoft.com> wrote in message
> news:05E34681-156E-41DA-B811-5159174C12E0@microsoft.com...
>>I need to compute a expression that user set as string in my project. It
>>will
>> look like:
>>
>> string s = "2*(a+5)/6";
>> and I need to compute a value of expression in string s.
>>
>> Is there some class in framework that solves this problem? Is a way how
>> solve it to use dynamic method?
>>
>> Thanks
>> Ivana
>
>

AddThis Social Bookmark Button