?
Solved

compile and execute in runtime with codecom and scripting with vb.net

Posted on 2014-01-08
8
Medium Priority
?
615 Views
Last Modified: 2014-02-16
Im gonna try to explain my question, please be patient because my english sucks

I would like to know if it is possible to compile and run elements of the code of my project in runtime?

For example in my project I have this loop
            Do
                id = id + 1 
                bandera = bandera + 1
                arreglo = valvula(iteraciones(id).indice)
            Loop Until (bandera = 100)

Open in new window

bandera and id are variables declared in a vb standard module in my project
arreglo it is an array declared in a vb standard module in my project
valvula is a function declared in a vb standard module in my project
iteraciones it is a "dictionary of" declared in a vb standard module in my project

I would like to know if it is possible to storage this code in a string and compile and execute that string in a runtime mode?
------------------------------------------------------------------------------------------------------------------------------
My first option was to use codecom

eric moreau send me this nice example: http://emoreau.com/Entries/Articles/2011/07/Compiling-code-on-the-fly.aspx it works, but I cannot use it because I dont know how to call functions, variables or custom class declared outside the namespace of the dynamic code

Thats way I  tried with scripting in vb.net (using msscriptcontrol) with this example:
Option Strict Off
Option Explicit On
Imports System.Collections.Generic
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim VBScript As MSScriptControl.ScriptControl
    Public valor As Double
    Public VBCode As String
    Public otro(2) As Double
    Public nuevo As New Dictionary(Of Integer, nodo)
    Public prueba As New nodo

    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
        On Error GoTo line1
        VBScript.AddCode(VBCode)
        Exit Sub
line1:
        MsgBox(Err.Number & Err.Description, MsgBoxStyle.Critical, "error")
        Err.Clear()
    End Sub

    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Dim prueba As New nodo
        otro(0) = 0
        prueba.uno = 1
        prueba.dos = 1
        nuevo.Add(1, prueba)
        VBCode = "form1.otro = form1.valvula(1,2,3)" & vbCr & _
"form1.valor = form1.funcion(13)" & vbCr & _
                 "MsgBox(Form1.valor)"
        VBScript = New MSScriptControl.ScriptControl
        VBScript.Language = "VBScript"
        VBScript.AddObject("Form1", Me)
        VBScript.AllowUI = True
    End Sub

    Function funcion(ByVal valores As Double) As Double
        valores = valores + 1
        funcion2()
        Return valores
    End Function

    Function valvula(ByVal dato1 As Integer, ByVal dato2 As Integer, ByVal dato3 As Integer) As Double()
        dato1 = dato1 + 1
        dato2 = dato2 + 1
        dato3 = dato3 + 1
        valvula = New Double() {dato1, dato2, dato3}
    End Function
End Class

Public Class nodo
    Public uno As Integer
    Public dos As Integer
End Class

Module Module1
    Public variable1 As Double = 120
    Public Sub funcion2()
        variable1 = variable1 + 1
        MsgBox(variable1)
    End Sub
End Module

Open in new window


It works to, but the problem is that when I try to use code lines like this:
"form1.otro(0)=1" or "form1.dictionary(index).id = 1"  the program cannot compile that kind of lines.
------------------------------------------------------------------------------------------


I would like to know what is the best option to use dynamic code

 if I use codecom if there is a way to call  variables, functions etc.. declared in my program from the dynamic code

And if I use scripting with vb.net why when I try to use lines like "form1.otro(0)=1" or "form1.dictionary(index).id = 1"  the program cannot compile that kind of lines.
0
Comment
Question by:altariamx2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39768847
eric moreau send me this nice example: http://emoreau.com/Entries/Articles/2011/07/Compiling-code-on-the-fly.aspx it works, but I cannot use it because I dont know how to call functions, variables or custom class declared outside the namespace of the dynamic code

Can you explain what you mean this statement?  Are you saying that you would be creating dynamic code that would be calling to static code elements?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39768898
Are you attempting to write some sort of parser ?

eg.  Your user enters things like:
Display 1 + 2*96 - 3*Foo(9)

and then your program works out what the resulting number will be (in this case 42) and then shows a message box with 'the answer is 42'

Or do you just need the user to enter eg. a number within a range and then you call a specific function (1 means square, 2 means square root....) ?
0
 

Author Comment

by:altariamx2003
ID: 39769638
yes Eric you are right

I want to create dynamic code that will call static code elements of my program (like variables or functions) to compile and execute in runtime or at least that is the idea

Im sorry if in the last post I did dont explain myselft better
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:altariamx2003
ID: 39769669
Hi Andy thanks to answer this post

What I need is that my program create dynamic code that will call statis code elements of my projects like variables, arrays, subroutines, functions etc... not just a specific function.

I need that my program create in a string for example
            Do
                id = id + 1 
                bandera = bandera + 1
                arreglo = valvula(iteraciones(id).indice)
            Loop Until (bandera = 100)

Open in new window

or
             Do
                id = id + 1 
                cadena(0) = bandera + 1
                arreglo = tanque(iteraciones(id).indice)
            Loop Until (cadena(0) = 100)

Open in new window

Use this dynamic code (that call static code elements) of my project and compile and execute it in runtime
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39770244
I'm going to bow out - that is a MASSIVE job (unless I completely misunderstand, and in that case my comments wouldn't be of use anyway).
0
 

Author Comment

by:altariamx2003
ID: 39771458
About scripting in .net what I need to know is why when I try to compile lines like "form1.otro = form1.valvula(1,2,3)" the program works

and when I try to use lines like "form1.otro = form1.valvula(myarray(0),2,3)" the program cannot compile this kind of lines
0
 

Accepted Solution

by:
altariamx2003 earned 0 total points
ID: 39852366
I need to apologies with eric moreu

He tried to help me in my previous post, but I did not understand his example until now

I found the way  to solve this big problem and all credits goes to eric, because without his example the solution that I found was not posible
0
 

Author Closing Comment

by:altariamx2003
ID: 39862504
I found the solution by mysself

and also

I need to apologies with eric moreu

He tried to help me in my previous post, but I did not understand his example until now

I found the way  to solve this big problem and all credits goes to eric, because without his example the solution that I found was not posible
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question