CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2009

    evaluate string as line of code

    Hi all,

    Firstly, I apologise if I am threading old water here - I had a brief search but could not find what I was looking for....

    I want to know if it is possible to evaluate a text string as if it is actual code, e.g.

    string equation = "";
    for (int i = 1; i <= 2; i++)
    equation += @"Convert.ToInt16(txtValue" + i.ToString() + @".Text) +";
    equation += " 0";
    lblEquals.Text = equation;

    output string: "Convert.ToInt16(txtValue1.Text) + Convert.ToInt16(txtValue2.Text) + 0"

    Basically, I want to evaluate the output string, as generated by the loop, as c# code as if I had just entered:

    int16 value = Convert.ToInt16(txtValue1.Text) + Convert.ToInt16(txtValue2.Text) + 0;

    I will not know, until run time, how many variables there are going to be but they will all be similar to txtValuen.Text

    Is this possible or am I barking up the wrong tree?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: evaluate string as line of code

    Most likely barking up the wrong tree.

    Why do want to do this?

  3. #3
    Join Date
    Jul 2009

    Re: evaluate string as line of code

    I am trying to write an ICM calculator that processes values based the inputs of a series of a series of boxes.
    The problem is that the process is pretty complicated and needs to be processed relative to previous calculations.

    Normally an algorithm is used, but I think that I can do it with formula's if I can build them independently and then process them.

  4. #4
    Join Date
    Oct 2004
    Rocket City

    Re: evaluate string as line of code

    Have you tried searching for 'expression evaluation'.

  5. #5
    Join Date
    Jul 2009

    Re: evaluate string as line of code

    Thanks zips - I will definitely check that out as it looks promising! It was partly the search term that I was struggling with....

    I have since moved on to reading the text boxes into arrays and am trying to process them that way but I may be able to go back to the formula idea....

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.