Results 1 to 6 of 6

Thread: Convert String to Expression

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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?

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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.

  4. #4
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    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.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    12

    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?

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. MessageBox.Show(AxScriptControl1.Eval(TextBox1.Text))
    Where TextBox1.Text is a string containing a function.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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