get output from string as passing parameter [Resolved]

Posted by Shoyebaziz123 under ASP.NET on 11/4/2015 | Points: 10 | Views : 1928 | Status : [Member] | Replies : 6
Hello,
How to calculate "((a+b)*1000)/(c-d)*6)" by passing this string as parameter so that I can get exact output
String is variable
Here we can change a b c d with some integer or decimal value
How it can b done ?

Responses

Posted by: Sheonarayan on: 11/4/2015 [Administrator] HonoraryPlatinum | Points: 50
 0 Do it differently, even if you pass that string as parameter you may not get what you are looking for. So, create a function that accepts a, b, c, d as parameters and return the calculated value from that function.`// declare functionprivate string MyResult(int a, int b, int c, int d){ var result = ((a+b)*1000)/(c-d)*6); return result.ToString();}// call nowvar myString = MyResult(2, 3, 4, 5);` Thanks, hope this helps.Regards, Sheo Narayan http://www.dotnetfunda.comShoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Shoyebaziz123 on: 11/4/2015 [Member] Starter | Points: 25
 1 No , all of the things are dynamic Means user can input a formula Eg (a+ b)*c) Through my code I can replace valuez of a b or c whatever Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 11/5/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 Try this`using Microsoft.CSharp;using System;using System.CodeDom.Compiler;using System.Data;using System.Reflection;using System.Text;namespace ConsoleApplication5{ class Program { static void Main(string[] args) { // object result = Eval("((10 + 12)*5)"); //result 110 object result = Eval("(((2+5)*1000)/((30-12)*6))"); //result 64 } private static object Eval(string sExpression) { CSharpCodeProvider c = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("system.dll"); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; StringBuilder sb = new StringBuilder(""); sb.Append("using System;\n"); sb.Append("namespace CSCodeEvaler{ \n"); sb.Append("public class CSCodeEvaler{ \n"); sb.Append("public object EvalCode(){\n"); sb.Append("return " + sExpression + "; \n"); sb.Append("} \n"); sb.Append("} \n"); sb.Append("}\n"); CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString()); if (cr.Errors.Count > 0) { throw new InvalidExpressionException( string.Format("Error ({0}) evaluating: {1}", cr.Errors[0].ErrorText, sExpression)); } Assembly a = cr.CompiledAssembly; object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler"); Type t = o.GetType(); MethodInfo mi = t.GetMethod("EvalCode"); object s = mi.Invoke(o, null); return s; } }}` -- Thanks & Regards, RNA TeamShoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 11/5/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 You can also try with Dynamic Expresso ( https://github.com/davideicardi/DynamicExpresso/ ) Dynamic Expresso is available on NuGet. You can install the package using: `PM> Install-Package DynamicExpresso.Core` Once done , then you can write the below ```using DynamicExpresso; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { var interpreter = new Interpreter(); //var result = interpreter.Eval("((10 + 12)*5)"); // result 110 var result = interpreter.Eval("(((2+5)*1000)/((30-12)*6))"); //result 64 } } }``` -- Thanks & Regards, RNA TeamShoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 11/5/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 You can also try out the project "A C# Expression Evaluator " ( http://www.blackbeltcoder.com/Articles/algorithms/a-c-expression-evaluator ). Looks promising-- Thanks & Regards, RNA TeamShoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator
Posted by: Rajnilari2015 on: 11/25/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25
 0 You can also try this article http://www.dotnetfunda.com/articles/show/3174/evaluate-an-expression-at-runtime-using-ironjs -- Thanks & Regards, RNA TeamShoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator