Evaluating A String as Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • david@davesaconsulting.com

    #1

    Evaluating A String as Code

    I have searched existing posts and have not found an answer to this
    variation of an old question. I have the following string stored in a
    variable

    Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
    else 0 End If"

    How do I evaluate/execute the string as code and return the answer, in
    this case: 1200?

    Previous examples of eval/execute have used the MSScriptControl to
    return the value of a numeric equation, but those did not have the If
    Then Else syntax. MSScriptControl generates syntax errors on my
    equation.

  • rowe_newsgroups

    #2
    Re: Evaluating A String as Code

    On May 16, 10:31 am, d...@davesacons ulting.com wrote:
    I have searched existing posts and have not found an answer to this
    variation of an old question. I have the following string stored in a
    variable
    >
    Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
    else 0 End If"
    >
    How do I evaluate/execute the string as code and return the answer, in
    this case: 1200?
    >
    Previous examples of eval/execute have used the MSScriptControl to
    return the value of a numeric equation, but those did not have the If
    Then Else syntax. MSScriptControl generates syntax errors on my
    equation.
    You could use regex (or instr etc) to split the string into If..Else
    blocks, evaluate the expressions with the MSScriptControl , and do the
    logical branching yourself. Unfortunately this is just a theory, and I
    don't any sample code to show you. (Though I might write some now that
    I'm intrigued).

    Thanks,

    Seth Rowe

    Comment

    • david@davesaconsulting.com

      #3
      Re: Evaluating A String as Code

      This is what I've been working on in the meantime:

      Dim str As String = "if %Sales_Volume_B ase <
      %Sales_Volume_B asis Then (%Price_Base - %Price_Swing ) *
      (%Sales_Volume_ Basis - %Sales_Volume_B ase) else 8 - 5 end if"

      Dim Return_Value As Double

      str = str.Replace("%S ales_Volume_Bas e", "9000")
      str = str.Replace("%S ales_Volume_Bas is", "10200")
      str = str.Replace("%P rice_Base", "6")
      str = str.Replace("%P rice_Swing", "5")

      Dim obj As New MSScriptControl .ScriptControl
      obj.Language = "VBScript"

      Dim sExpression As String = str.ToUpper

      Dim iPosIf As Integer = InStr(sExpressi on, "IF")
      Dim iPosThen As Integer = InStr(sExpressi on, "THEN")
      Dim iPosElse As Integer = InStr(sExpressi on, "ELSE")
      Dim iPosEndIf As Integer = InStr(sExpressi on, "END IF")


      If iPosIf 0 Then
      'if there is an IF THEN ELSE statement
      Dim d As Double
      d = obj.Eval(str.Su bstring(iPosIf + 2, iPosThen - 5))

      'determine the results of the THEN clause
      If d = True Then
      If iPosElse 0 Then
      Return_Value = obj.Eval(str.Su bstring(iPosThe n +
      4, iPosElse - iPosThen - 6))
      Else
      If iPosEndIf 0 Then
      Return_Value = obj.Eval(str.Su bstring(iPosThe n
      + 4, iPosEndIf - iPosThen - 6))
      Else
      Return_Value = obj.Eval(str.Su bstring(iPosThe n
      + 4))
      End If
      End If
      End If

      'if there is an ELSE clause, find the results
      If iPosElse 0 Then
      If d = False Then
      If iPosEndIf 0 Then
      Return_Value = obj.Eval(str.Su bstring(iPosEls e
      + 4, iPosEndIf - iPosElse - 6))
      Else
      Return_Value = obj.Eval(str.Su bstring(iPosEls e
      + 4))
      End If
      End If
      Else
      Return_Value = 0
      End If

      Else
      'if there is no IF THEN ELSE
      Return_Value = obj.Eval(sExpre ssion)
      End If

      MsgBox(Return_V alue)

      Comment

      • Miro

        #4
        Re: Evaluating A String as Code

        Is this what you guys are looking for
        -im too new to get into this stuff yet ...but i beleive it is what you are
        looking for.

        I do something simillar in another programing language - execute/build
        things at runtime not compile.



        i seem to recall an old post from months ago someone did the same using
        vbscript

        Miro



        <david@davesaco nsulting.comwro te in message
        news:1179332414 .957864.251230@ h2g2000hsg.goog legroups.com...
        This is what I've been working on in the meantime:
        >
        Dim str As String = "if %Sales_Volume_B ase <
        %Sales_Volume_B asis Then (%Price_Base - %Price_Swing ) *
        (%Sales_Volume_ Basis - %Sales_Volume_B ase) else 8 - 5 end if"
        >
        Dim Return_Value As Double
        >
        str = str.Replace("%S ales_Volume_Bas e", "9000")
        str = str.Replace("%S ales_Volume_Bas is", "10200")
        str = str.Replace("%P rice_Base", "6")
        str = str.Replace("%P rice_Swing", "5")
        >
        Dim obj As New MSScriptControl .ScriptControl
        obj.Language = "VBScript"
        >
        Dim sExpression As String = str.ToUpper
        >
        Dim iPosIf As Integer = InStr(sExpressi on, "IF")
        Dim iPosThen As Integer = InStr(sExpressi on, "THEN")
        Dim iPosElse As Integer = InStr(sExpressi on, "ELSE")
        Dim iPosEndIf As Integer = InStr(sExpressi on, "END IF")
        >
        >
        If iPosIf 0 Then
        'if there is an IF THEN ELSE statement
        Dim d As Double
        d = obj.Eval(str.Su bstring(iPosIf + 2, iPosThen - 5))
        >
        'determine the results of the THEN clause
        If d = True Then
        If iPosElse 0 Then
        Return_Value = obj.Eval(str.Su bstring(iPosThe n +
        4, iPosElse - iPosThen - 6))
        Else
        If iPosEndIf 0 Then
        Return_Value = obj.Eval(str.Su bstring(iPosThe n
        + 4, iPosEndIf - iPosThen - 6))
        Else
        Return_Value = obj.Eval(str.Su bstring(iPosThe n
        + 4))
        End If
        End If
        End If
        >
        'if there is an ELSE clause, find the results
        If iPosElse 0 Then
        If d = False Then
        If iPosEndIf 0 Then
        Return_Value = obj.Eval(str.Su bstring(iPosEls e
        + 4, iPosEndIf - iPosElse - 6))
        Else
        Return_Value = obj.Eval(str.Su bstring(iPosEls e
        + 4))
        End If
        End If
        Else
        Return_Value = 0
        End If
        >
        Else
        'if there is no IF THEN ELSE
        Return_Value = obj.Eval(sExpre ssion)
        End If
        >
        MsgBox(Return_V alue)
        >

        Comment

        • Spam Catcher

          #5
          Re: Evaluating A String as Code

          david@davesacon sulting.com wrote in news:1179325891 .246076.76020
          @k79g2000hse.go oglegroups.com:
          I have searched existing posts and have not found an answer to this
          variation of an old question. I have the following string stored in a
          variable
          >
          Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
          else 0 End If"
          >
          How do I evaluate/execute the string as code and return the answer, in
          this case: 1200?
          >
          Previous examples of eval/execute have used the MSScriptControl to
          return the value of a numeric equation, but those did not have the If
          Then Else syntax. MSScriptControl generates syntax errors on my
          equation.

          Are you trying to run VB code, VBA code or something else?

          ..NET has the ability to dynamically compile code and execute. I've done
          that before to create an application scripting engine.

          Comment

          • Tom Shelton

            #6
            Re: Evaluating A String as Code


            <david@davesaco nsulting.comwro te in message
            news:1179325891 .246076.76020@k 79g2000hse.goog legroups.com...
            >I have searched existing posts and have not found an answer to this
            variation of an old question. I have the following string stored in a
            variable
            >
            Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
            else 0 End If"
            >
            How do I evaluate/execute the string as code and return the answer, in
            this case: 1200?
            >
            Previous examples of eval/execute have used the MSScriptControl to
            return the value of a numeric equation, but those did not have the If
            Then Else syntax. MSScriptControl generates syntax errors on my
            equation.

            David,

            If you are using VB.NET code, then you can use CodeDOM to compile and
            execute that code at runtime. Look at System.CodeDOM.

            --
            Tom Shelton

            Comment

            • david@davesaconsulting.com

              #7
              Re: Evaluating A String as Code

              On May 16, 1:54 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
              d...@davesacons ulting.com wrote in news:1179325891 .246076.76020
              @k79g2000hse.go oglegroups.com:
              >
              I have searched existing posts and have not found an answer to this
              variation of an old question. I have the following string stored in a
              variable
              >
              Dim str as String = "If 9000 < 10200 Then (6 - 5 ) * (10200 - 9000)
              else 0 End If"
              >
              How do I evaluate/execute the string as code and return the answer, in
              this case: 1200?
              >
              Previous examples of eval/execute have used the MSScriptControl to
              return the value of a numeric equation, but those did not have the If
              Then Else syntax. MSScriptControl generates syntax errors on my
              equation.
              >
              Are you trying to run VB code, VBA code or something else?
              >
              .NET has the ability to dynamically compile code and execute. I've done
              that before to create an application scripting engine.
              Doing this in vb.net.

              Comment

              • Spam Catcher

                #8
                Re: Evaluating A String as Code

                david@davesacon sulting.com wrote in news:1179406905 .660240.121860
                @q75g2000hsh.go oglegroups.com:
                >Are you trying to run VB code, VBA code or something else?
                >>
                >.NET has the ability to dynamically compile code and execute. I've done
                >that before to create an application scripting engine.
                >
                Doing this in vb.net.
                So the "scripting language" is VB.NET?

                Comment

                Working...