[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!
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!
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.
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!