Results 1 to 4 of 4

Thread: [RESOLVED] converting a string to code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    73

    Resolved [RESOLVED] converting a string to code

    VS 2008.
    Hey guys. I was trying to figure out how to convert a string (that the user would input to the program via a textbox) into actual code for the program to compile. For example, if the user was to enter: (5*100)+(20-3). How could you take that inputted string, and have the program solve it and return the value? If someone could also explain the code too, because I'd like to tinker with it to use it for things such as inserting other forms of code as well. Thanks for the help!

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: converting a string to code

    There are two questions there really : a) how can I compile user entered text and b) how do I convert a mathematical equation into code.

    If you follow this link you'll find help on how to accept text at runtime and compile it. The second part I'll leave for someone else!

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: converting a string to code

    With a quick google search, I found a few ways to do it. One way that is kind of a cool hack is using the datatable:

    Code:
            Dim d As New DataTable()
            MessageBox.Show(d.Compute("(5*100)+(20-3)", Nothing).ToString())
    Source

    The Compute method has some good documentation from which you should be able to figure out its limitations.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    73

    Re: converting a string to code

    Negative0, thank you for that. That code block works great! as far as the other post, i'll have to look into it in more detail, i kind of just skimmed over it. this is close enough though, thanks guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width