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 8767991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:57:27+00:00 2026-06-13T16:57:27+00:00

I’m trying to load a VB source file into memory. However, the VB file

  • 0

I’m trying to load a VB source file into memory. However, the VB file assumes that Project it is associated to has some global “Imported Namespaces” defined at the project level. This VB feature allows individual files to omit the Imports statement (Using in C#) on every single file.

    Dim sourceCode As String = ""
    'sourceCode &= "Imports System.Data" & vbNewLine
    sourceCode &= "Class Foo" & vbNewLine
    sourceCode &= "Sub Print()" & vbNewLine
    sourceCode &= "Dim dtbl As DataTable" & vbNewLine
    sourceCode &= "System.Console.WriteLine(""Hello, world!"")" & vbNewLine
    sourceCode &= "End Sub" & vbNewLine
    sourceCode &= "End Class" & vbNewLine

    Dim compiler As New Microsoft.VisualBasic.VBCodeProvider

    Dim params As New Compiler.CompilerParameters
    params.ReferencedAssemblies.Add("System.dll")
    params.ReferencedAssemblies.Add("System.Data.dll")
    params.ReferencedAssemblies.Add("System.Xml.dll")
    params.GenerateInMemory = True
    params.GenerateExecutable = False

    Dim results As Compiler.CompilerResults = compiler.CompileAssemblyFromSource(params, sourceCode)

    If results.Errors.Count > 0 Then
        For Each compileError In results.Errors
            Console.WriteLine(compileError.ToString)
        Next
        Return
    End If

    Dim assembly = results.CompiledAssembly

Line 2 is commented out. If I uncomment this and add the Imports statement the code works fine. It also works fine if I change “Dim dtbl As DataTable” to “Dim dtbl As System.Data.DataTable”.

Instead of uncommenting that line of code, is there a way to feed this Imports statement into the compiler or params as if it was a global project level Imported Namespace?

I could just add this Imports statement to the top of each file I read in. But if it is already there then I get an error that the Imports statement is duplicate. I could do some Regex checking to see if the Imports statement is already there, but I’d like to leverage the System.CodeDom framework as much as possible.

  • 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-13T16:57:29+00:00Added an answer on June 13, 2026 at 4:57 pm

    OK, no answers 🙁 I guess the framework doesn’t do what I’d like to do. Here is my hacky solution using Regex to inject the Imports statement.

    sourceCode = AddImportsIfNeeded(sourceCode, "System.Data")
    
    
    Private Function AddImportsIfNeeded(ByVal sourceCode As String, ByVal namespaceToImport As String) As String
    
        If Not Regex.IsMatch(sourceCode, "^\s*Imports\s+" & Regex.Escape(namespaceToImport) & "\s*$", RegexOptions.Multiline) Then
            Return "Imports " & namespaceToImport & vbNewLine & sourceCode
        End If
        Return sourceCode
    
    End Function
    

    Note that this won’t work if the file contains Option statements (like Option Strict On). The Imports statements must go below the Option statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm trying to create an if statement in PHP that prevents a single post

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.