Resolved Converting user input text field into logic

iori2500

New member
Joined
Sep 24, 2020
Messages
2
Programming Experience
5-10
Hi,

Is there a way for C# to convert user input text into a programming logic

e.g.

Validate( "ABC = XYZ" ) -> returns FALSE
Validate( "ABC = ABC" ) -> returns TRUE
Validate( "10 + 20 - 5" ) -> returns 25
Validate( "(ABC = ABC) AND (10 + 20 = 15 + 15) " ) -> returns TRUE (this is more complex, but you get the idea)

Thinking of Python-like single line logic validation...
 
Neither C#, or the .NET Framework doesn't have a readily available eval() function where you can just pass in any random expression. Traditionally, people would use the classes and methods in the CodeDom namespace of the .NET Framework to dynamically compile and run code, but there are newer options with building expression trees, or using Roslyn.

Those maybe overkill, though. It possible that the expressions you want to evaluate maybe simple enough that you don't need to dynamically compile and run code. You maybe able to just symbolically do the evaluation like one of the standard CS exercises for infix to postfix, and then followed by postfix evaluation.
 

Latest posts

Back
Top Bottom