4

I'm trying to execute some code inside a string in runtime. I.E.

Dim code As String = "IIf(1 = 2, True, False)"

How do i run the code inside code string ?

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
Developer
  • 41
  • 1
  • 3
  • Use the **CodeDom** compiler classes. https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.vbcodeprovider(v=vs.110).aspx I have a wrapper that you can analyze: https://github.com/ElektroStudios/ElektroKit/blob/master/Solution/v1.5/Elektro.Interop/Types/VisualBasicCompiler.vb If you only want to evaluate arithmetic expressions, then I'll suggest to use **NCalc** library: https://ncalc.codeplex.com/ (because Microsoft's JS evaluator is deprecated). Also, you should use **IF()** instead **IIF()**. – ElektroStudios Mar 24 '16 at 12:49
  • Thank you. I guess i'll give it a try using CodeDom using this method: http://stackoverflow.com/questions/21379926/load-vb-net-code-from-txt-file-and-execute-it-on-fly-using-system-codedom-compi – Developer Mar 28 '16 at 08:16

1 Answers1

3

As @ElektroStudios said - the proper way to do this is to use the CodeDom compiler, but this is a bit overkill for something as simple as this.

You can sort of cheat and harness the power of a DataColumn Expression

So for example:

    Dim formula = "IIF(Condition = 'Yes', 'Go', 'Stop')"
    Dim value As String = "Yes"
    Dim result As String

    'add a columns to hold the value
    Dim colStatus As New DataColumn
    With colStatus
        .DataType = System.Type.GetType("System.String")
        .ColumnName = "Condition"
    End With

    'add a column to compute the expression
    Dim colExp As New DataColumn
    With colExp
        .DataType = System.Type.GetType("System.String")
        .ColumnName = "Expression"
        .Expression = formula
    End With

    'create a table and add the columns
    Dim dt As New DataTable
    With dt.Columns
        .Add(colStatus)
        .Add(colExp)
    End With

    'now add a row and set the condition to the value we have
    Dim row As DataRow = dt.NewRow
    row.SetField(Of String)("Condition", value)
    dt.Rows.Add(row)

    'now read back the computed value based on the expression being evaluated
    result = row.Field(Of String)("Expression")
    MessageBox.Show(result)

You could wrap all this up into a more generic function like this:

Public Function EvaluateExpression(Of T, K)(input As T, formula As String) As K
    'add a columns to hold the value
    Dim colStatus As New DataColumn
    With colStatus
        .DataType = GetType(T)
        .ColumnName = "Condition"
    End With

    'add a column to compute the expression
    Dim colExp As New DataColumn
    With colExp
        .DataType = GetType(K)
        .ColumnName = "Expression"
        .Expression = formula
    End With

    'create a table and add the columns
    Dim dt As New DataTable
    With dt.Columns
        .Add(colStatus)
        .Add(colExp)
    End With

    'now add a row and set the condition to the value we have
    Dim row As DataRow = dt.NewRow
    row.SetField(Of T)("Condition", input)
    dt.Rows.Add(row)

    'now read back the computed value based on the expression being evaluated
    Return row.Field(Of K)("Expression")
End Function

So then you can call it like this:

Dim result = EvaluateExpression(Of Integer, Boolean)(1, "IIF(Condition = 1, True, False)")
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 2
    Using a datacolumn expression its really a smart solution that I never seen before. thanks for share this. I noted that for strings the user needs to supply a single quote instead double-quote in the condition. (**Condition = 'string'**), just I wanted to comment that, because I was asking me why the double-quote escape was failling, then I tried with a single quote. – ElektroStudios Mar 25 '16 at 12:58