Let a user write an expression as a string and return a value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Federico Caselli

    Let a user write an expression as a string and return a value

    I'm using visual basic.net, I need to take in input a string containing an
    expression and return the value, example:

    "14=14" must return true
    "14+14" must return 28
    "8<4" must return false
  • Morten Wennevik

    #2
    Re: Let a user write an expression as a string and return a value

    Hi Federico,

    I believe you will have to do this manually the hard way by splitting
    operators and numbers by presedence and possibly recursively.

    On Fri, 21 Jan 2005 00:43:04 -0800, Federico Caselli
    <FedericoCasell i@discussions.m icrosoft.com> wrote:
    [color=blue]
    > I'm using visual basic.net, I need to take in input a string containing
    > an
    > expression and return the value, example:
    >
    > "14=14" must return true
    > "14+14" must return 28
    > "8<4" must return false[/color]



    --
    Happy Coding!
    Morten Wennevik [C# MVP]

    Comment

    • Steven Spits

      #3
      Re: Let a user write an expression as a string and return a value

      "Federico Caselli" wrote:
      [color=blue]
      > I'm using visual basic.net, I need to take in input a string containing an
      > expression and return the value, example:[/color]

      This is possible by generating code, compile it and execute, all at runtime!

      I've done it, works like a charm! This article got me started:



      Steven

      - - -


      Comment

      • Federico Caselli

        #4
        Re: Let a user write an expression as a string and return a value

        Thank you. The class Evaluator is really what I need. Just one thing: I need
        to evaluate a boolean expression, like "84 < 70" or "7 >=8". So I tried to
        add the following method to the class Evaluator
        -------------------------------------------------------------------------------------
        public static bool EvalToBoolean(s tring statement)
        {
        string s = EvalToString(st atement);
        return bool.Parse(s.To String());

        -------------------------------------------------------------------------------------

        When I try to use the method

        blnRes = Evaluator.EvalT oBoolean("7<8")

        I get an exception.

        Is there a way to make it work?

        Thank you




        "Steven Spits" wrote:
        [color=blue]
        > "Federico Caselli" wrote:
        >[color=green]
        > > I'm using visual basic.net, I need to take in input a string containing an
        > > expression and return the value, example:[/color]
        >
        > This is possible by generating code, compile it and execute, all at runtime!
        >
        > I've done it, works like a charm! This article got me started:
        >
        > http://www.odetocode.com/Code/80.aspx
        >
        > Steven
        >
        > - - -
        >
        >
        >[/color]

        Comment

        Working...