0

I'm working on a project where the user needs to have the ability to create a formula in a WPF app. The formula has to be made from some properties. Let's say we have properties:

public decimal? Prop1 { get; set; }
public decimal? Prop2 { get; set; }
public decimal? Prop3 { get; set; }

Now, let's say we have a TextBox where the user has to be able to input only those properties and in between of the there needs to be some of the operators: +, -, /, *.

I got the part where the user can only input those values properties and operators… But my problem is how can I save that formula to the SQL database (lets say Prop1 * Prop2), how can I get the formula from database and make it read the values from the and basically "do the math…"

decimal? x = MyFormula;

I know the question is difficult, and so is my description of my it… Please feel free to ask for more detail if needed, and I'll try to explain it.

sharp
  • 1,191
  • 14
  • 39
  • You can run javascript formula in C#, just need to replace PropX placeholders with values from properies. Look into this https://stackoverflow.com/questions/4744105/parse-and-execute-js-by-c-sharp for more details. – just-my-name Dec 07 '18 at 14:27
  • @just-my-name Yes, basically that's what I want, but I'm searching for that "replace"... That's with what I'm struggling – sharp Dec 07 '18 at 14:49
  • I've used ironpython for what sounds like a similar requirement. If it fits what you want to do it is easy to implement. https://weblogs.asp.net/dfindley/Using-IronPython-for-Dynamic-Expressions_2E00_ – Andy Dec 07 '18 at 15:42

1 Answers1

1

There's two distinct questions there, and they're both a bit open ended.

My suggestion for your first question would be to serialise the Model / ViewModel to a string. You might find that you want to store them slightly differently. For example:

public class Property
{
    public string PropReference{get;set;}
}

public class Operator
{
    public string Operator{get;set;}
}

public class Calculation
{
    public Property Prop {get;set;}
    public Operator Op {get;set;}
    public Property Prop2 {get;set;}
}

That way you can chain them together, and even nest them. In answer to your second question, you might want to investigate a library such as Jace, as this isn't an easy problem, but has already been solved.

EDIT - Alternative solution based on comments:

public class Operator : IAction
{
    public string Operator {get;set;}
}

public class Calculation : IAction
{
    IList<KeyValuePair<IAction, Property>> Operations {get;set;}
}
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • Yes, I was thinking about similar, but the difference is that there can be an > 1 operator or even none. And the calculation too, more than one Property and none or more types of Calculation – sharp Dec 07 '18 at 13:10
  • You could potentially make the Proprty/Operator a kvp list, which would easily allow chaining. If you also need nesting you could simply reference a Calculation from within Calculation – Paul Michaels Dec 07 '18 at 13:15