Paste the code to a VB file to run. I just give a thinking, according to this idea, I can make a strong calculator or make a code tester or an exercise device.
Option Strict Off
Imports system.io
Imports system.codedom.compiler
Imports system.reflection
Namespace lzmtw.test
Public Class frMCal
Inherits System.Windows.Forms.form
Private Code As New CodeBuilder
Private mymode as mode
#Region "Windows Form Designer Generated Code"
Public Sub New ()
Mybase.new ()
'This call is required for the Windows Form Designer.
InitializeComponent ()
'Add any initialization after INITIALIZECOMPONENT ()
End Sub
'Form rewriting Dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (Byval Disposing as Boolean)
IF Disposing then
IF not (Components Is Nothing) THEN
Components.dispose ()
END IF
END IF
Mybase.dispose (Disposing)
End Sub
'Windows Form Designer
Private Components as System.comPonentModel.icontainer
'Note: The following process is necessary for the Windows Form Designer.
'You can modify this process using the Windows Form Designer.
'Don't modify it using the code editor.
Friend Withevents Tbcode as System.Windows.Forms.TextBox
Friend Withevents TBRESULT As System.Windows.Forms.TextBox
Friend Withevents Btncal As System.Windows.Forms.Button
Friend WitHevents Tbreturnpara as system.windows.Forms.TextBox
Friend Withevents Label1 As System.Windows.Forms.Label
Friend Withevents Label2 As System.Windows.Forms.Label
Friend Withevents TBSUB As System.Windows.Forms.TextBox
Friend Withevents Label3 As System.Windows.Forms.label
Friend Withevents Label4 As System.Windows.Forms.Label
Friend Withevents Button1 As System.Windows.Forms.Button
Private
Sub initializecomponent ()
Me.tbcode = new system.windows.Forms.TextBox
Me.tbresult = new system.windows.Forms.TextBox
Me.btncal = new system.windows.Forms.Button
Me.Tbreturnpara = new system.windows.Forms.TextBox
Me.Label1 = new system.windows.forms.labelme.label2 = new system.windows.Forms.Label
Me.tbsub = new system.windows.Forms.TextBox
Me.label3 = new system.windows.Forms.Label
Me.label4 = new system.windows.forms.label
Me.Button1 = new system.windows.Forms.Button
Me.suspendlayout ()
'
'TBCode
'
Me.tbcode.Anchor = ctype (((System.Windows.Forms.Anchorstyles.top or system.windows.Forms.Anchorstyles.bottom) _
OR system.windows.forms.Anchorstyles.left)
Or System.Windows.Forms.Anchorstyles.right, System.Windows.Forms.Anchorstyles
Me.tbcode.location = new system.drawing.point (8, 32)
Me.tbcode.multiline = true
Me.tbcode.name = "tbcode"
Me.tbcode.scrollbars = system.windows.Forms.Scrollbars.both
Me.tbcode.size = new system.drawing.size (584, 72)
Me.tbcode.tabindex = 0
Me.TBCode.Text = "'gived radius, survey of perimeter" & Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "DIM M (3)" & Microsoft.VisualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) & "M (0) = 20" & Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "m (1) = m (0) * Math.pi * 2 "& Microsoft.VisualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) &" M (2) = Math.pi * Math.Pow (M (0), 2) "& Microsoft .Visualbasic.chrw (13) & microsoft.visualbasic.chrw (10) & "d" & _
"Im Result" & Microsoft.visualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) & "Result =" radius: "" m (0) .tostring VBCRLF "" Zhou Chang: " M (1) .tostring VBCRLF "" face "& _
"Product:" M (2) .tostring "& Microsoft.visualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) &" & Microsoft.VisualBasic.chrw (13) & Microsoft.visualBasic.chrw 10) & "" & Microsoft.visualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) & "Open Excel" & Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "DIM OEXCEL AS Excelobj "& Microsoft.VisualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) &" OExcel "& _". Open "& Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "OEXCEL.GETDATA (Result)
'
'TBRESULT
'
Me.tbresult.Anchor = ctype (((System.Windows.Forms.Anchorstyles.bottom or system.windows.forms.Anchorstyles.Left) _
Or System.Windows.Forms.Anchorstyles.right, System.Windows.Forms.Anchorstyles
Me.tbresult.location = new system.drawing.point (8, 360)
Me.tbresult.multiline = true
Me.tbresult.name = "TBRESULT"
Me.tbresult.scrollbars = system.windows.Forms.Scrollbars.both
Me.tbresult.size = new system.drawing.size (584, 64)
Me.tbresult.tabindex = 1
Me.tbresult.text = ""
'
'btncal
'
Me.btncal.Anchor = ctype ((System.Windows.Forms.Anchorstyles.Bottom or System.Windows.Forms.Anchorstyles.right), System.Windows.Forms.Anchorstyles)
Me.btncal.location = new system.drawing.point (448, 320)
Me.btncal.name = "btncal"
Me.btncal.size = new system.drawing.size (120, 32)
Me.btncal.tabindex = 2
Me.btncal.text = "Calculation (& C)"
'
'TBRETURNPARA
'
Me.tbReturnPara.Anchor = CType ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.tbReturnPara.Location = New System.Drawing.Point (104, 112)
Me.tbreturnpara.name = "tbreturnpara"
Me.tbreturnpara.size = new system.drawing.size (152, 21)
Me.tbreturnpara.tabindex = 3
Me.Tbreturnpara.text = "result"
'
'Label1
'
Me.label1.anchor = ctype ((System.Windows.Forms.Anchorstyles.Bottom or System.Windows.Forms.Anchorstyles.Left), System.Windows.Forms.Anchorstyles
Me.label1.location = new system.drawing.point (8, 112)
Me.Label1.name = "label1"
Me.label1.size = new system.drawing.size (72, 16)
Me.label1.tabindex = 4
Me.Label1.Text = "Return value parameters"
'
'Label2
'
Me.label2.location = new system.drawing.point (8, 8)
Me.Label2.name = "label2"
Me.label2.size = new system.drawing.size (128, 16)
Me.Label2.tabindex = 5
Me.label2.text = "Main program:"
'
'TBSUB
'
Me.tbsub.anchor = ctype ((System.Windows.Forms.Anchorstyles.bottom or system.windows.forms.Anchorstyles.left) _
Or System.Windows.Forms.Anchorstyles.right, System.Windows.Forms.Anchorstyles
Me.tbsub.location = new system.drawing.point (8, 176)
Me.tbsub.multiline = true
Me.tbsub.name = "TBSUB"
Me.tbsub.scrollbars = system.windows.Forms.Scrollbars.both
Me.tbsub.size = new system.drawing.size (584, 136)
Me.tbsub.tabindex = 6
Me.Tbsub.Text = "Class Excelobj" & Microsoft.visualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) & "Private Shared Obj, WB, WS" & Microsoft.visualBasic.chrw (13) & Microsoft. VisualBasic.chrw (10) & "Public Shared Sub Open" & Microsoft.visualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "OB" & _ "J = CreateObject (" Excel.Application " "& Microsoft.visualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) &" obj.visible = true "& microsoft.visualbasic.chrw (13) & microsoft.visualbasic.chrw (10) &" End Sub " & Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "P" & _
"Ublic Shared Sub getData (Value) & Microsoft.VisualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) &" IF WB is Nothing the "& Microsoft.VisualBasic.chrw (13) & Microsoft.visualBasic.chrw (10) & "WB = Obj." & _
"Workbooks.add" & Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "WS = worksheets.add" & Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) ) & "Endiff" & microsoft.visualbasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & "ws.cells (1" & _
", 1) = Value" & Microsoft.visualbasic.chrw (13) & Microsoft.visualBasic.chrw (10) & "End Sub" & Microsoft.VisualBasic.chrw (13) & Microsoft.VisualBasic.chrw (10) & " "END CLASS"
'
'Label3
'
Me.Label3.Anchor = CType ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.Label3.Location = New System.Drawing.Point (8, 152)
Me.label3.name = "label3"
Me.label3.size = new system.drawing.size (128, 16)
Me.label3.tabindex = 7
Me.Label3.Text = "function:"
'
'Label4
'
Me.Label4.anchor = ctype ((System.Windows.Forms.Anchorstyles.Bottom or System.Windows.Forms.Anchorstyles.left), System.Windows.Forms.Anchorstyles
Me.label4.location = new system.drawing.point (8, 336)
Me.label4.name = "label4"
Me.label4.size = new system.drawing.size (128, 16)
Me.label4.tabindex = 8
Me.Label4.Text = "Result:"
'
'Button1
'
Me.button1.anchor = ctype ((System.Windows.Forms.Anchorstyles.Bottom or System.Windows.Forms.Anchorstyles.right), System.Windows.Forms.Anchorstyles
Me.button1.location = new system.drawing.point (240, 320)
Me.Button1.name = "button1"
Me.Button1.size = new system.drawing.size (120, 32)
Me.Button1.taBindex = 9
Me.Button1.text = "View Code"
'
'frmcal
'
Me.autoscalebasesize = new system.drawing.size (6, 14)
Me.ClientSize = new system.drawing.size (600, 429)
Me.Controls.add (me.button1)
Me.Controls.add (me.label4)
Me.Controls.add (me.label3)
Me.Controls.add (me.tbsub)
Me.Controls.add (me.label2)
Me.Controls.add (me.label1)
Me.Controls.add (me.tbreturnpara)
Me.Controls.add (me.btncal)
Me.Controls.add (me.tbresult)
Me.Controls.add (me.tbcode)
Me.Name = "frMcal"
Me.Text = "Simple Code Calculator"
Me.ResumeLayout (false)
End Sub
#End region
Private sub btncal_click (Byvale as system.Object, byval e as system.eventargs) Handles btncal.clickcode.clear ()
DIM I as INT16
For i = 0 to tbcode.lines.lendth - 1
Code.Appendcode (2, TBCode.Lines (i))
NEXT
Code.Appendcode (2, "return" & me.tbreturnpara.text)
Code.Appendcode (1, "End Function")
Code.Appendcode ()
For i = 0 to TBSub.Lines.Length - 1
Code.Appendcode (2, TBSub.Lines (i))
NEXT
MyMode = New MODE
mymode.getmode (code.toString)
'Msgbox (mymode.codestring)
Me.tbresult.text = ""
Me.tbresult.text = mymode.calResult
End Sub
Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click
IF not mymode is nothing then
Me.tbresult.text = ""
Me.tbresult.text = mymode.codestring
END IF
End Sub
END CLASS
Public Class Mode
Private comp as icodecompiler = new vbcodeProvider (). CreateCompiler
Private CP as CompilerParameters = New CompilerParameters
Private _compiled as object = Nothing
Private mi as methodinfo
Private mycode as cotebuilder
Public Readonly Property Codestring () AS String
Get
Return mycode.toString
END GET
End Property 'Codestring
Sub new ()
End Sub
SUB getMode (byval modestring as string)
Mycode = New CodeBuilder
With mycode
.Appendcode ("imports system")
.Appendcode ("Imports System.Data)
.Appendcode ("Imports System.math")
.Appendcode ("Imports Microsoft.visualBasic")
.Appendcode ()
.Appendcode ("Public Class Dealmode")
.Appendcode ("Public Function GETM ()")
.Appendcode (modestring)
'.Appendcode ("end function")
.Appendcode ("End Class")
End with
End sub 'getModepublic function calresult () AS Object
cp.referencedassemblies.add ("system.dll")
cp.referencedassemblies.add ("system.data.dll")
cp.referencedassemblies.add ("system.xml.dll")
cp.referencedassemblies.add ("Microsoft.VisualBasic.dll")
cp.generateexecutable = false
cp.generateInmemory = TRUE
'DIM CodeBuilding AS
DIM Code as String
Code = mycode.toString
DIM CR AS CompilerResults = Comp.compileAssEmblyFromSource (CP, CODE)
Debug.write (code.toString)
IF (cr.errors.haserrors) THEN
Dim ErrorMessage As String
ErrorMessage = "Compile error:" & vbcrlf
Dim Err As CompileRrror
For Each Err in Cr.ErrorS
ErrorMessage = ErrorMessage & Err.ErrorText & Vbcrlf
NEXT
Debug.writeLine (ErrorMessage)
#If debug the
STOP
#ELSE
Throw New Exception ("Compiling Error:" ErrorMessage)
#End IF
END IF
Dim a as system.reflection.assembly = cr.compiledassembly
_Compiled = a.createInstance ("dealmode")
Mi = _compiled.gettype (). getMethod ("getm")
Return mi.invoke (_compiled, nothing)
END FUNCTION
END CLASS 'MODE
Public Class CodeBuilder
Private _stringbuilder as system.text.stringbuilder
Private const codeformat as string = "{0} {1}" & controlchars.crlf
Sub new ()
_StringBuilder = new system.text.stringbuilder
End Sub
Public overloads sub appendcode ()
_StringBuilder.AppendFormat (CodeFormat, Space (0), Space (0))
End Sub
Public overloads sub appendcode (Byval Codestring As String)
_StringBuilder.AppendFormat (CodeFormat, Space (0), CODEString)
End Sub
Public overloads sub appendcode (byval codefloor as integer)
_StringBuilder.AppendFormat (CodeFormat, Space (Codefloor * 4), Codestring) End Sub
Public Sub appendfromfile (Byval FileName As String)
IF not system.io.file.exists (filename) THEN
MsgBox (FileName & "does not exist.")
EXIT SUB
END IF
DIM TMPSTR AS STRING
DIM FS as system.IO.FileStream
Fs = new system.io.filestream (filename, io.filemode.open, io.fileAccess.read, oosed)
Dim Reader As New System.io.StreamReader (fs, system.text.encoding.default)
TMPSTR = Reader.ReadToend
Reader.close ()
fs.close ()
_StringBuilder.Append (tmpstr)
End Sub
Public overrides function toString () AS STRING
Return_stringbuilder.tostring
END FUNCTION
Public Sub Clear ()
IF_StringBuilder.Length> 0 Then _stringbuilder.remove (0, _StringBuilder.Length - 1)
End Sub
End Class' CodeBuilder
End Namespace