|
-
May 10th, 2007, 01:55 PM
#1
Thread Starter
New Member
Convert String to Expression
Suppose I have a textbox called inputfunction and a label called outputfunction. I press a button and this is the code
Dim variable as Object
variable = inputfunction.Text
outputfunction.Text = variable
Ok, If I type in the number 4 in the textbox, I get 4 in the label
What I want is to type in a mathematical expression as a string, and make my "variable" not a string but into a segment of code. Basically I want it so when I type in "2 + 2" it will give me 4 in the label. When I type in sqrt(4) I want it to give me 2 in the label instead of an error message. How do I convert the String into a mathematical expression that visualbasic.net can read as a code insertion?
-
May 10th, 2007, 02:27 PM
#2
Re: Convert String to Expression
Welcome aboard!
First, I would use a collection of command buttons with built-in functions rather than try to interpret what someone types in a textbox. In short, your program then becomes somewhat of a pre-programmed calculator but it maintains control of the manipulations.
Note that Excel and other spreadsheets do somewhat what you propose, but if a user makes one mistake in the formula expression, he errors out. However, if you want to try that, go ahead. Use an = sign at the beginning or the end to delimit the activity. But, your code must "look" at what has been typed and then try to figure out as closely as possible what the user wants. In short, it isn't going to be a cakewalk.
-
May 10th, 2007, 02:29 PM
#3
Thread Starter
New Member
Re: Convert String to Expression
ok, say the user is someone who knows proper code entry.
In short, I want the string written in the textbox to actually be read as code from vb.net. I know the language Python does this easily.
-
May 10th, 2007, 02:46 PM
#4
Frenzied Member
Re: Convert String to Expression
It's not easy in .Net, and I'm not sure if it can do it to the extent that you want it done. But what you want to do is called reflection. This might be a good place to start reading up on reflection.
http://www.codeproject.com/vb/net/Do...lerArticle.asp
But I suggest doing some searches on Reflection, and you might find your answer.
-
May 10th, 2007, 04:25 PM
#5
Thread Starter
New Member
Re: Convert String to Expression
[URL=http://forums.devx.com/showthread.php?t=145021[/URL]
Check the above url. Someone posted a solution to my problem that sounds very easy to do. This was posted.
Hi Hazara,
Why don't you use microsoft ScriptControl ActiveX. Add this activeX in your toolbox. It will be found in COM components. Put this on a form and use its Evaluate function to get the results !!!
Ravinder Verma
So how do I go about doing that?
-
May 10th, 2007, 05:23 PM
#6
Re: Convert String to Expression
- Right click your toolbox
- Select 'Choose items...'
- Click the 'COM Components' tab
- Scroll down and find 'ScriptControl object' and check it
- Place it on your form.
Heres an example on how to use it:
VB Code:
MessageBox.Show(AxScriptControl1.Eval(TextBox1.Text))
Where TextBox1.Text is a string containing a function.
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
|