|
-
Sep 21st, 2009, 02:50 PM
#1
Thread Starter
Lively Member
[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!
-
Sep 21st, 2009, 02:53 PM
#2
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!
-
Sep 21st, 2009, 04:36 PM
#3
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.
-
Sep 21st, 2009, 10:46 PM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|