Results 1 to 4 of 4

Thread: Execute code at runtime

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    2

    Execute code at runtime

    Does VB.net allow for a way of executing a string of code at runtime?

    I've seen code using System.CodeDom.Compiler but it needs to compile an entire VB object and run it seperatley from the program. I just need to execute one line. (I have seen vb6 code that does this but that was 2 years ago)

    Basically i'm trying to grab a form name from a database and display the form.

    Thanks in advance.

  2. #2
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103

    Re: Execute code at runtime

    Does VB.net allow for a way of executing a string of code at runtime?

    Basically i'm trying to grab a form name from a database and display the form.
    Sure!

    You can do this using Reflection.
    It does not compile a thing, just retrieve informations about objects and methods into assemblies and let you call them at runtime.

    Take a look to this example:

    VB Code:
    1. Assembly a = Assembly.Load(strMyAssemblyFile);
    2. Type     oType = a.GetType(strCompleteNamespaceAndClassName);
    3. object   oMyObj = Activator.CreateInstance(oType);
    4.  
    5. MethodInfo oMethod = oType.GetMethod(strMethodName);
    6. oMethod.Invoke(oMyObj, new object[] {MyParam});

    Bye!
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    If the forms are within his project not an external assembly he can simply use this:
    VB Code:
    1. Dim frm As Form = Activator.CreateInstance(Type.GetType("Namespace.FormName"))
    2. frm.Show()
    Where NameSpace.FormName is the full name of the form type he wants to use.
    Last edited by Lunatic3; Aug 18th, 2003 at 05:24 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    One API function can does this , it was a little challenging on VB6 forum , look it up there , you may find it .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width