Solved

dynamic code using vb.net

Posted on 2014-01-05
10
433 Views
Last Modified: 2014-02-11
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

0
Comment
Question by:altariamx2003
  • 6
  • 3
10 Comments
 
LVL 12

Expert Comment

by:ktaczala
ID: 39758080
what are the conditions that would require the need to "Compile" & execute conditionally?
0
 

Author Comment

by:altariamx2003
ID: 39758137
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
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 39758306
0
 

Author Comment

by:altariamx2003
ID: 39759982
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?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39760009
>>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!
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:altariamx2003
ID: 39760758
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?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39760868
my vbscript is too many moons away for me to remember. sorry.
0
 

Author Comment

by:altariamx2003
ID: 39765994
Well thanks anyway
0
 

Author Comment

by:altariamx2003
ID: 39766752
Im gonna  to rephrase my question I hope somebody else help me

thanks anyway
0
 

Author Comment

by:altariamx2003
ID: 39852369
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
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now