Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

String Evaluation in C#

Back to Forum: Tech Off
  • User profile image
    DavidBarrett

    Hey,

    Im working on a project where users will be entering a value into a text box on a page, the system then has to go off to the database and determine what calculation to run on that value based on what the user has entered a typical calculation would be:

    (n * 0.53702)/1000 = a

    Where n is the value that the user has entered and a is the answer that I want. The issue is that these calculations vary depending on the type of data.. and can be changed frequenty so we want to give the users a way to manage these.

    There will always only be one input from the user and one output but the other bits may differ, Some of the other calcs that we need are :

    (n * 0.206)/1000 = a
    (n * 0.447) = a

    I'd rather not hardcode these if possible.. as I said they are likley to change and Id rather not have to re-compile and re-deploy every time a calculation changes.

    Any thoughts?

  • User profile image
    lensman

    Well, the contents of your textbox are in a String.  When you note that the action depends upon the "type" of the data, what do you mean?  What defines a type to you?

     

    If you mean type as in .NET types then you could try some tryparse expressions to test if the value is in fact the expected type.

    I.E.,

    Int32 ivalue = 0;

    if (Int32.TryParse(textbox.text, out ivalue) ==  true) {

       // valid integer value

    } else

    {

       // not valid integer, try second type or display error message

    }

     

    Obviously if your meaning of the word "type" differs than so will your solution.

  • User profile image
    DavidBarrett

    , lensman wrote

    Well, the contents of your textbox are in a String.  When you note that the action depends upon the "type" of the data, what do you mean?  What defines a type to you?

     

    If you mean type as in .NET types then you could try some tryparse expressions to test if the value is in fact the expected type.

    I.E.,

    Int32 ivalue = 0;

    if (Int32.TryParse(textbox.text, out ivalue) ==  true) {

       // valid integer value

    } else

    {

       // not valid integer, try second type or display error message

    }

     

    Obviously if your meaning of the word "type" differs than so will your solution.

     

    I meant type in the non code sense.. however I have found the answer (well I found an answer, it may not be the only one) I have downloaded the FLEA library from codeplex and it allows me to pass in a string expression "(a * 1000) / 100)" and assign a value to a and have the calculation run:

     

    Simple example:

    

     ExpressionContext context = new ExpressionContext();
                
    IGenericExpression<decimal> eGeneric = context.CompileGeneric<decimal>(tb.Text);
    
    decimal result = (decimal)eGeneric.Evaluate();
                
    Response.Write(result.ToString());  

     

    In the case above the text box (tb) can contain something as simple as 1 + 1 which will output 2 (duh!) or it can contain the formula that I specify with a placeholder for the dynamic data entered by the user... Seems to cover off what I need at the moment.

    Thanks for the response.

  • User profile image
    xgamer

    If I understand your problem correctly, you have range of values and as per the range of values the formula changes. The formula itself is dynamic and you wish it could be user-defined or easy to change.

    So I assuming you wish a scenario like

    If the user enter "1" then the formula to calculate output is  (n * 0.53702)/1000
    If the user keysin "2-15" then the formula for output  is say   (n * 0.447 + n ) and so on ...

    I would suggest the following

    a. Create a setup screen - where user can define the data range and formula with a system defined constant "X" to represent where user input will be placed in formula:
    For example:
    Range : 2-25
    Formula : (X * 0.447 + X )  

    Do some easy but smart verification on the formula text box such as two "X" are not beside each other there is other common maths  symbols separating values etc. May be even include a check to do a calculation on the basis of one of the random values within the range to check correctness of formula.

    b. Once the user enters the value in your Calculation using Simple string replacement create string representing the value in the formula For example : if the user enters 7 the string will look like ( 7 * 0.447 + 7 )

    c. After this you can use Math Expression parser like http://ncalc.codeplex.com/ to evaluate the expression.

    Hope this was what you were looking for ...

  • User profile image
    nittu

    You can use one of Extension method that Cinchoo library provides as below

    Console.WriteLine("Output of {100 + 20}: " + "100 + 20".Evaluate());

    Output:

    Output of {100 + 20}: 120

    URL:

    http://cinchoo.wordpress.com/2012/01/11/cinchoo-string-extension-methods-evaluate/

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.