Link to home
Start Free TrialLog in
Avatar of altariamx2003
altariamx2003Flag for Mexico

asked on

dynamic code using vb.net

Hi

I Hope that all of you have a happ new year

I hope you can help me with this problem

I would like to know if it is possible to execute elements of my project dynamically in vb.net

I have several parts of my code that I need to compile and execute them in runtime, and I would like to know if it is possible to do that

I found several examples on internet wher I execute dynamic code and show messages

But in does examples I cannot use variables, functions, etc.. any element outside the dynamic code

it is possible to execute dynamically code with variables, functions, loops or whatever i need ??

for example this portion of my code:
            Do
                iteraciones = iteraciones + 1
                bandera = bandera + 1
                vm_1era_etapa_p1 = valvula(CDbl(grados_api_p1.Text), entrada_sistema_p1(0), entrada_sistema_p1(1), entrada_sistema_p1(2), entrada_sistema_p1(3), entrada_sistema_p1(4), entrada_sistema_p1(5), CDbl(rec_agua21_p1.Text), rec2a1ppm_p1, rec2a1temp_p1)
            Loop Until (bandera = 100)

Open in new window

Avatar of ktaczala
ktaczala
Flag of United States of America image

what are the conditions that would require the need to "Compile" & execute conditionally?
Avatar of altariamx2003

ASKER

The main idea is that if the program can create vb code using variables, arrays of the same program, for example:

for a = 0 to 1
                    iteraciones = iteraciones + 1
                bandera = bandera + 1 
next

Open in new window

or
iteraciones = iteraciones + 1
bandera = bandera + 1 

Open in new window


compile the code created and use it as normal

I found several examples on internet like this: http://www.codemag.com/Article/0211081

But in all this example I need to create a new namespace and I cannot use anything of my program (variables, arrays, functions,etc..) because I presume they are in another namespace
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice example Eric, I thing that is what I need

But I have a few questions

The only way to use variables or other stuff is sending as a parameters???,

What happend if I have global variables declared in a module?? can I use them without sending as a parameters???

On last question

I have this subroutine in my  form1

    Public Shared Sub Testing()
        MsgBox("test")
    End Sub

How can I call it inside the dynamic call???
I was thinking something like:
            .AppendLine("    Public Shared Function CalcSalesTaxes(ByVal pState As String, ByVal pAmount As Decimal) As Decimal ")
            .AppendLine("         Form1.Testing()")
            .AppendLine("        Dim decRate As Decimal ")

Open in new window


But it doesnt work, any ideas?
>>What happend if I have global variables declared in a module??

AND

>>I have this subroutine in my  form1
>>How can I call it inside the dynamic call???

You just cannot. The dynamic code cannot see the other code. It runs in its own memory space and doesn't have access to the caller stuff!
ok

What if I do it with script?

I made this example
Option Strict Off
Option Explicit On
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim VBScript As MSScriptControl.ScriptControl
    Public valor As Double
    Public VBCode As String

    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 Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
        Text1.Text = ""
    End Sub

    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        VBCode = "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
        Return valores
    End Function
End Class

Open in new window

This previous code works
----------------------------------------------------------------
I can call the function "funcion" and asigned his value to the variable "valor" inside the dynamic code, but this only works for the content of form1,
VBScript.AddObject("Form1", Me)

Open in new window


I would like to do the same for vb modules, to use their content, it is possible to use it with modules?
my vbscript is too many moons away for me to remember. sorry.
Well thanks anyway
Im gonna  to rephrase my question I hope somebody else help me

thanks anyway
I need to apologies with you eric moreu

You tried to help me with your example , but I did not understand it until now

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

thanks a lot Eric