Evaluate Code Stored in String

How can I evaluate / execute code that I have stored in a string

I have many textboxes named: tb1, tb2, tb3, etc.

I want to run code like this:

For i = 1 to 3

temp1 = "Me.tb" & i & ".Value"

temp2 = [EVALUATE or EXECUTE](temp1)

Next

Any suggestions / helps Please also let me know which libraries need to be included. Any help is appreciated.

Thank you!




Answer this question

Evaluate Code Stored in String

  • simonoch

    I am not really sure how you would apply that to my code.  If I have the following, what should I change/add

    'There are 5 text boxes on the form (Text0 - Text4).  This code adds the values of Text0 to Text3 and enters the sum into Text4.

    Private Sub getSum_Click()
        Dim i As Integer
        Dim Text As String
        Dim sum As Integer
        sum = 0
        For i = 0 To 3
            Text = "Me.Text" & i & ".Value"
            sum = sum + evaluateCMD(Text)
        Next i
        Me.Text4.Value = sum
    End Sub
    Private Function evaluateCMD(Text As String) As Integer
        Dim temp As String
    'Needless to say it doesn't work, this is where I get an error.  It says that it can't find the name 'Me' I entereed in the expression.
        temp = Eval(Text)
        evaluateCMD = CInt(temp)
    End Function



  • pietpeters

    You can also use something like:

    Dim sum As Integer
    sum = 0

    For i As Integer = 0 To 3
        If IsNumeric(Me.Controls("Text" & i).Text) Then
            sum += CInt(Me.Controls("Text" & i).Text)
        End If
    Next i

    Me.Text4.Text = sum


  • Taylor Brown

    oh. . . I thought you wanted to evaluate an expression that was typed in a text box. . . you want to get a control by name.

    given you have TextBox1, TextBox2, TextBox3, TextBox4 and TextBox5 and a button Button1 on a form:

    Imports System.Reflection
    Public Class Form1
    Private Function TextBoxByNumber(ByVal n As Integer) As TextBox
    Dim fi As System.Reflection.FieldInfo = _
    Me.GetType().GetField("_TextBox" & n.ToString(), _
    BindingFlags.GetField
    Or _
    BindingFlags.Instance
    Or _
    BindingFlags.NonPublic)
    Return fi.GetValue(Me)
    End Function

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
    Handles Button1.Click
    Dim i As Integer
    Dim sum As Integer = 0
    For i = 1 To 4
    sum += Convert.ToInt32(TextBoxByNumber(i).Text)
    Next i
    Me.TextBox5.Text = sum
    End Sub
    End
    Class

    For brevity, I have omitted type checking and error handling.



  • Maplesoft

    I was mistaken as to how to access an object loaded into the control. Its actually like this:

    Dim result as object = MSScriptControl.Modules(0).Eval("ObjectID.Value")

    Its been a couple of years since I used it at such a basic level. I have it wrapped up in a Script Library component.

    check out this example - ScriptDemo.zip

    Its a much more simplified example of using the script control. . . Make sure you have the latest version of the script control. Check your msscript.ocx against this one.

    Script control only runs in x86 targeted framework. it will run on x64, but not compiled in x64 app.

    The script control is a real strong thing to have on your tool belt. You can publish your objects to your end user so they can dynamically automate your application.

    cheers!



  • SathikKhan

    ahh thats right. . . Windows Controls will work off of name. . . I forgot. . . thought they only worked off of reference.

    be careful. . . if the control is on another container in the form, it wont work though, will it



  • andcrumx

    Take a look at the MSScript control.

    I would like to see exactly what you are trying to do. MSScript is quite powerful but it takes some learning. Its not hard, just takes a little practice.

    You need to make an interface of what you want to publish.

    Load the an object instance that implements the interface into the MSScriptControl with an id into a module.

    then

    Dim result as object = MSScriptControl.Modules("TheModule").ObjectID.Value

    But this is the answer, so mark it.

    I wrote an app for someone to run and evaluate response performance http web testing scripts that uses the MSScript control. It can be found here

    Its in .Net 1.1 and C#. See my profile for my email if you need help.



  • Evaluate Code Stored in String