Eval()

  • Thread starter Thread starter J.B.
  • Start date Start date
J

J.B.

Hi all.
I'm rather new to C#, (just learning ;-) and i'm making my own 'helper'
library, in wich i want to
have an eval(statement) function.

Anyone know how to make this / help me out on where to start?
Thanks.
 
Something like
string eval(operand1, operand2, operator) { ... } ?

Basically, you could have different eval methods for different operand
types, such as:
string eval(int1, int2, operator) {}
string eval(double1, double2, operator) {}
....

Of course you could also use a function that simply takes object arguments:
string eval(object1, object2, operator) {...}

Within that particular method you could then simply check for the actual
type of the arguments (using GetType()) and proceed accordingly.

The operator would be a value representing any one of the opertors you would
like to support, such as "+", "-", "/", etc.

In the method body you then require a switch statement in which you check
for the actual operator and perform the appropriate operation with the
operands. You would then return a string representation of the result as the
result of the Eval() function.

Does that get you started?

Regards,
Alex
 
Back
Top