2

Background

We currently have an excel-based system for the creation of specifications for a sound and lighting rental company.

Part of this is a column in the excel sheet called 'autospec', which is made up of Excel formulas for individual stock items ( e.g., you specify a loudspeaker, and then the formulas in the autospec column calculate the cables you need and add them onto the specification automatically.

sample formula

10m microphone cable might have a formula like the following:

=IF([loudspeakers]>0,[loudspeakers]*2,0)+([mixing desk]*4)

We're now moving over to a proper database with a C# front end.

Question

What I would like is to be able to store the autospec formulas for each stock item in a table, and when the user specs an item the front end, the program should find the relevant formula, execute it, and change the spec quantity as appropriate.

Bottom line: I need to execute code contained within a string.

Am I going about this the wrong way? Is there a better way?

Community
  • 1
  • 1
Jez Clark
  • 2,919
  • 4
  • 26
  • 31
  • 3
    I'd tend to write your rules in C#. You have a good programming language with a good IDE, why not use it? – CodesInChaos Jan 26 '11 at 14:41
  • Yeah that's what I intend to do, the Excel formula was just to illustrate what we USED to do. 8 years of pain have brought me to the point of hatred of Excel for anything other than simple number crunching. – Jez Clark Jan 26 '11 at 17:47

2 Answers2

1

I asked a similar question once: How can I evaluate a C# expression dynamically?

You could probably use that to evaluate those expressions. This not really a safe thing to do, unless you can guarantee nobody will be adding junk (read: evil code) to your database.

If you can break down the formulas into "families", such that each entry in your formula column is a member of a small (5-10) set of formulas with just different parameters, you could try something like this:

[ItemTable]<-[ItemFormulaParameters(param1, param2, param3)]->[FormulaTable(name)]

And have a factory method for instantiating the formula objects by name. Each such formula object has a "calculate(param1, param2, param3)" property...

Community
  • 1
  • 1
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
0

Either build you're own code interprenter or build a 'dynamic rule engine' that creates some binary representation of c# rules that can be executed by a 'rule engine' you'd have to write.

CodingBarfield
  • 3,392
  • 2
  • 27
  • 54