title: 'TestString' is not declared. It may be inaccessible due to its protection level. (BC30451)
body: <p>I am trying out some on the fly code compilation using the <code>VBCodeProvider</code> class. What I want to be able to do is modify a public variable in my assembly in the code.</p>
  
  <p>I have a <code>Public TestString As String = ""</code> in my application.</p>
  
  <p>This is the code I am using to compile:</p>
  
  <pre><code>    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 &gt; 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
  </code></pre>
  
  <p>This is the code in my textbox:</p>
  
  <pre><code>Imports System
  Imports MyAssembly
  
  Class Script
      Sub Main()
         TestString="foo"' &lt;-- This line fails compilation
      End Sub
  End Class
  </code></pre>
  
  <p>The error I get is <strong>'TestString' is not declared. It may be inaccessible due to its protection level. (BC30451)</strong></p>