Hi,
I'd like to convert a string to a variable formula in C#. I mean that I wanna write as the following.
string formula="a*b";
int a=10;
int b=10;
int ans= formula //here I want to convet formula to.........
Is there any way to solve my problem? Please help me.
Loading

VulpesPosted Aug 5, 2011, 4:58 AM
In that case, I'd suggest making the highlighted alteration to these lines when using integers which you want to be treated as doubles:
Andry LebedevPosted Oct 3, 2011, 1:46 AM
IExpression expression = BaseExpression.Parse("x/(95+y)");
IExpression result = expression.Execute();
VulpesPosted Aug 5, 2011, 5:39 AM
Previously, COM methods which returned a Variant, returned this to C# code as an 'object'. You then had to cast it back to the exact underlying type or you'd get an exception.
VS 2010 (C# 4.0) now uses the 'dynamic' type in this scenario which is more forgiving than 'object'. Basically, if the conversion is valid at runtime, then it's allowed regardless of the underlying type. In fact you don't even need a cast any more. This line works fine in VS 2010:
double c = sc.Eval(formula); // no cast needed!
Pravin GhadgePosted Aug 5, 2011, 5:10 AM
Your solutions has solved the problem
I am using VS 2005.
VulpesPosted Aug 5, 2011, 4:19 AM
Are you using VS 2010 and v1.0 of the ScriptControl?
Pravin GhadgePosted Aug 5, 2011, 4:13 AM
I have used ur code as follows
ScriptControl sc=new ScriptControl();
Its giving foll. error:
"Specified cast is not valid."
What mistake i have done?
VulpesPosted Aug 5, 2011, 3:45 AM
If you're trying to do this for arbitrarily complex formulae, then you need to use an Eval() function such as the one provided by VBScript which can be used from C#.
To do this, start a new console application project in VS and add a reference to the Microsoft Script Control 1.0 which can be found under the COM tab.
Now enter the following code, build and run:
using System.Text;
using MSScriptControl;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var sc = new ScriptControl();
sc.Language = "VBScript";
string formula = "a*b";
int a = 10;
int b = 10;
formula = formula.Replace("a", a.ToString());
formula = formula.Replace("b", b.ToString());
int c = (int)sc.Eval(formula);
Console.WriteLine("Result: " + c);
Console.ReadKey();
}
}
}
Pravin GhadgePosted Aug 5, 2011, 3:41 AM
Because i am keeping formula in formula master table.
In formula master, i have used 2 fields:1)Formula_Name & 2) Formula_String
EG:
Formula MAster Table:
FormulaName Formula_String
SquareArea Width*Width
RectangleArea Width*Length
Jiteendra SampathiraoPosted Aug 5, 2011, 2:50 AM
int a = 10;
int b = 20;
int x = a * b;
string y = Convert.ToString(x);
int z = Convert.ToInt32(y);
Pravin GhadgePosted Aug 5, 2011, 2:33 AM
I have tried ur code. bt it is not solving my issues.
Jiteendra SampathiraoPosted Aug 5, 2011, 2:23 AM
string y = "result is" + a * b+ "Hope this help you";
Pravin GhadgePosted Aug 5, 2011, 2:05 AM
Actually iam trying to fix formula in string & then calculating values using that formula.
Is it possible?
Eg:
It giving me foll error:
"Input string was not in a correct format."
Plz Plz help me out.
Jiteendra SampathiraoPosted Aug 5, 2011, 1:51 AM
int a = 10;
int b = 20;
int x = a * b;
Console.WriteLine("Result:" + x);