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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:04:02+00:00 2026-06-09T12:04:02+00:00

I am trying out some on the fly code compilation using the VBCodeProvider class.

  • 0

I am trying out some on the fly code compilation using the VBCodeProvider class. What I want to be able to do is modify a public variable in my assembly in the code.

I have a Public TestString As String = "" in my application.

This is the code I am using to compile:

    Dim codeProvider As New VBCodeProvider()
    Dim optParams As New CompilerParameters
    optParams.ReferencedAssemblies.Add("MyAssembly.exe")
    optParams.ReferencedAssemblies.Add("system.windows.forms.dll")
    optParams.CompilerOptions = "/t:library"
    optParams.GenerateInMemory = True
    Dim results As CompilerResults = codeProvider.CompileAssemblyFromSource(optParams, RichTextBox1.Text)

    If results.Errors.Count > 0 Then
        Dim sb As New StringBuilder("Compilation Failed with the following error(s)" + CR_LF + CR_LF)
        For Each CompErr As CompilerError In results.Errors
            sb.Append(String.Format("Line {0} - {1} ({2}){3}", CompErr.Line, CompErr.ErrorText, CompErr.ErrorNumber, CR_LF))
        Next
        MessageBox.Show(sb.ToString, "Compile Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        Dim assy As System.Reflection.Assembly = results.CompiledAssembly
        Dim exeinstance As Object = assy.CreateInstance("Script")
        Dim typ As Type = exeinstance.GetType
        Dim method As MethodInfo = typ.GetMethod("Main")
        method.Invoke(exeinstance, Nothing)
    End If

This is the code in my textbox:

Imports System
Imports MyAssembly

Class Script
    Sub Main()
       TestString="foo"' <-- This line fails compilation
    End Sub
End Class

The error I get is ‘TestString’ is not declared. It may be inaccessible due to its protection level. (BC30451)

  • 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-09T12:04:03+00:00Added an answer on June 9, 2026 at 12:04 pm

    Just like normal VB.NET, as well as adding a reference, you have to Imports the relevant namespace, or specify it fully. (You’ve edited the question to now include this.)

    After inserting your code into a new Console project in VS2008 (because that’s what I had open) and adjusting for my assembly names, I saw the same as you.

    I fixed it by adding Public to the default Module Module1.

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

Sidebar

Related Questions

I was just trying out some code, not a experienced coder. I implemented(at least
I was trying out some exercises of db-class.org videos.There was one problem of finding
I was trying out some c++ code while working with classes and this question
For trying out some pointer stuff, I made an instance variable in an existing
I've been trying out some of the view code from Erica Sadun's book The
I'm learning Objective-C and just trying out some sample code. I am getting the
I trying out some problems in Google's Code Jam. For the question Store Credit
I am trying to generate some XML on-the-fly in Scala. I want to use
I'm trying out some dynamic web page background generation using lines and text. Take
Was trying out some code with the EF code first method and ran into

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.