470,590 Members | 1,788 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

Code Run Dynamically From Textbox in Vb.Net

Hello Friends

I run dynamically code from text-box in vb.net
only one text-box txtoutput can be access in dynamically run code

while get input from another text-box compiler show error
"txtbox1 is not declared .it may be inaccessible due to its protection level"


Please help me how to resolve this error

Thanks in Advance
Attached Images
File Type: jpg Run Test Ok.jpg (82.8 KB, 265 views)
File Type: jpg error textbox.jpg (90.0 KB, 179 views)
Attached Files
File Type: txt Dynamic Code.txt (2.5 KB, 180 views)
Nov 27 '19 #1
1 3007
SioSio
269 256MB
Add textBox1 and txtBox2 to the array of objects representing the arguments passed to the method (UpdateText).
And add textBox1 and txtBox2 to the argument of subroutine UpdateText.

Expand|Select|Wrap|Line Numbers
  1. Public Sub runCode()
  2.         ' Read code from file  
  3.         '  Dim input = My.Computer.FileSystem.ReadAllText("Code1.txt")
  4.         Dim input = TextBox3.Text
  5.         ' Create "code" literal to pass to the compiler.  
  6.         '  
  7.         ' Notice the <% = input % > where the code read from the text file (Code.txt)   
  8.         ' is inserted into the code fragment.  
  9.         Dim code = <code>  
  10.                        Imports System  
  11.                        Imports System.Windows.Forms  
  12.                        Public Class TempClass  
  13.                            Public Sub UpdateText(ByVal txtOutput As TextBox, ByVal txtbox1 As TextBox, ByVal txtbox2 As TextBox)  
  14.                                <%= input %>  
  15.                            End Sub
  16.                        End Class 
  17.                    </code>
  18.         ' Create the VB.NET compiler.  
  19.         Dim vbProv = New VBCodeProvider()
  20.             ' Create parameters to pass to the compiler.  
  21.             Dim vbParams = New CompilerParameters()
  22.             ' Add referenced assemblies.  
  23.             vbParams.ReferencedAssemblies.Add("mscorlib.dll")
  24.             vbParams.ReferencedAssemblies.Add("System.dll")
  25.             vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  26.             vbParams.GenerateExecutable = False
  27.             ' Ensure we generate an assembly in memory and not as a physical file.  
  28.             vbParams.GenerateInMemory = True
  29.             ' Compile the code and get the compiler results (contains errors, etc.)  
  30.             Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, code.Value)
  31.             ' Check for compile errors  
  32.             If compResults.Errors.Count > 0 Then
  33.                 ' Show each error.  
  34.                 For Each er In compResults.Errors
  35.                     MessageBox.Show(er.ToString())
  36.                 Next
  37.             Else
  38.                 ' Create instance of the temporary compiled class.  
  39.                 Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
  40.             ' An array of object that represent the arguments to be passed to our method (UpdateText).  
  41.             Dim args() As Object = {Me.txtoutput,Me.txtbox1,Me.txtbox2}
  42.             ' Execute the method by passing the method name and arguments.  
  43.             Dim t As Type = obj.GetType().InvokeMember("UpdateText", BindingFlags.InvokeMethod, Nothing, obj, args)
  44.         End If
  45.     End Sub
Dec 18 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Senthil Kumar via .NET 247 | last post: by
4 posts views Thread by Jaime Stuardo | last post: by
3 posts views Thread by kj | last post: by
15 posts views Thread by =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post: by
reply views Thread by =?Utf-8?B?amVmZmVyeQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.