Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:04:14+00:00 2026-06-18T11:04:14+00:00

Is it event possible to import text as code, then add it in a

  • 0

Is it event possible to import text as code, then add it in a sub in vb.net?
If I have a .txt file filled with code can I import it programatically (by a button)?

What I need is to make vb.net to accept that script (txt file), and use it to declare variables and make functions/subs – that’s all I need to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:04:16+00:00Added an answer on June 18, 2026 at 11:04 am

    You can do this kind of thing using the CodeDom objects. The CodeDom objects allow you to dynamically generate assemblies at run-time. For instance, if you make an interface

    Public Interface IScript
        Property Variable1 As String
        Sub DoWork()
    End Interface
    

    Then, you create a method, like this:

    Imports Microsoft.VisualBasic
    Imports System.CodeDom.Compiler
    
    ' ...
    
    Public Function GenerateScript(code As String) As IScript
        Using provider As New VBCodeProvider()
            Dim parameters As New CompilerParameters()
            parameters.GenerateInMemory = True
            parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location)
            Dim interfaceNamespace As String = GetType(IScript).Namespace
            Dim codeArray() As String = New String() {"Imports " & interfaceNamespace & Environment.NewLine & code}
            Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, codeArray)
            If results.Errors.HasErrors Then
                Throw New Exception("Failed to compile script")
            Else
                Return CType(results.CompiledAssembly.CreateInstance("Script"), IScript)
            End If
        End Using
    End Function
    

    Now, you can call it like this:

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim builder As New StringBuilder()
        builder.AppendLine("Public Class Script")
        builder.AppendLine("    Implements IScript")
        builder.AppendLine("    Public Property Variable1 As String Implements IScript.Variable1")
        builder.AppendLine("    Public Sub DoWork() Implements IScript.DoWork")
        builder.AppendLine("        Variable1 = ""Hello World""")
        builder.AppendLine("    End Sub")
        builder.AppendLine("End Class")
        Dim script As IScript = GenerateScript(builder.ToString())
        script.DoWork()
        MessageBox.Show(script.Variable1) ' Displays "Hello World"
    End Sub
    

    Obviously, instead of building the code in a string builder, you could load it out of a text file, like this:

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim script As IScript = GenerateScript(File.ReadAllText("C:\script.txt")
        script.DoWork()
        MessageBox.Show(script.Variable1) ' Displays "Hello World"
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a txt file, and I can import it into Excel with the
Is it possible to add button click event by clicking design of axml? if
Is it possible to handle a mouseDown event in VB.Net (2008) regardless of the
how is possible to synchonize text typed into JTextField (then by DocumentListener passed String
I have a game here, and I would like to add event handlers to
I have a JTextComponent in which a user can enter text in two ways:
is it possible to somehow import C# code into Microsoft Visio 2010 to generate
Is that possible to fire Flash-button click-event via javascript ? it`s my code ,
Possible Duplicate: What does the "Multiple markers" mean? package test2; import java.awt.*; import java.awt.event.*;
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.