Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / Visual Basic

Compiling with CodeDom

Rate me:
Please Sign up or sign in to vote.
4.48/5 (22 votes)
19 May 20042 min read 130K   2.3K   52  
This article explains how to compile code written in VB.NET or C# using the CodeDom.
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
   Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents txtAssembly As System.Windows.Forms.TextBox
   Friend WithEvents btnAssemblyAdd As System.Windows.Forms.Button
   Friend WithEvents lstAssemblies As System.Windows.Forms.ListBox
   Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
   Friend WithEvents chkInMemory As System.Windows.Forms.CheckBox
   Friend WithEvents chkDebug As System.Windows.Forms.CheckBox
   Friend WithEvents Label2 As System.Windows.Forms.Label
   Friend WithEvents txtMainClass As System.Windows.Forms.TextBox
   Friend WithEvents Label3 As System.Windows.Forms.Label
   Friend WithEvents chkErrors As System.Windows.Forms.CheckBox
   Friend WithEvents txtWarningLevel As System.Windows.Forms.TextBox
   Friend WithEvents Label4 As System.Windows.Forms.Label
   Friend WithEvents Label5 As System.Windows.Forms.Label
   Friend WithEvents txtResource As System.Windows.Forms.TextBox
   Friend WithEvents txtCompilerOptions As System.Windows.Forms.TextBox
   Friend WithEvents Label6 As System.Windows.Forms.Label
   Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
   Friend WithEvents lstCodeFiles As System.Windows.Forms.ListBox
   Friend WithEvents btnCodeAdd As System.Windows.Forms.Button
   Friend WithEvents txtCodeToAdd As System.Windows.Forms.TextBox
   Friend WithEvents Label7 As System.Windows.Forms.Label
   Friend WithEvents btnCompile As System.Windows.Forms.Button
   Friend WithEvents chkExe As System.Windows.Forms.CheckBox
   Friend WithEvents txtOutput As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.GroupBox1 = New System.Windows.Forms.GroupBox
      Me.lstAssemblies = New System.Windows.Forms.ListBox
      Me.btnAssemblyAdd = New System.Windows.Forms.Button
      Me.txtAssembly = New System.Windows.Forms.TextBox
      Me.Label1 = New System.Windows.Forms.Label
      Me.GroupBox2 = New System.Windows.Forms.GroupBox
      Me.txtCompilerOptions = New System.Windows.Forms.TextBox
      Me.Label6 = New System.Windows.Forms.Label
      Me.txtResource = New System.Windows.Forms.TextBox
      Me.Label5 = New System.Windows.Forms.Label
      Me.txtWarningLevel = New System.Windows.Forms.TextBox
      Me.Label4 = New System.Windows.Forms.Label
      Me.chkErrors = New System.Windows.Forms.CheckBox
      Me.txtOutput = New System.Windows.Forms.TextBox
      Me.Label3 = New System.Windows.Forms.Label
      Me.txtMainClass = New System.Windows.Forms.TextBox
      Me.Label2 = New System.Windows.Forms.Label
      Me.chkDebug = New System.Windows.Forms.CheckBox
      Me.chkInMemory = New System.Windows.Forms.CheckBox
      Me.chkExe = New System.Windows.Forms.CheckBox
      Me.GroupBox3 = New System.Windows.Forms.GroupBox
      Me.lstCodeFiles = New System.Windows.Forms.ListBox
      Me.btnCodeAdd = New System.Windows.Forms.Button
      Me.txtCodeToAdd = New System.Windows.Forms.TextBox
      Me.Label7 = New System.Windows.Forms.Label
      Me.btnCompile = New System.Windows.Forms.Button
      Me.GroupBox1.SuspendLayout()
      Me.GroupBox2.SuspendLayout()
      Me.GroupBox3.SuspendLayout()
      Me.SuspendLayout()
      '
      'GroupBox1
      '
      Me.GroupBox1.Controls.Add(Me.lstAssemblies)
      Me.GroupBox1.Controls.Add(Me.btnAssemblyAdd)
      Me.GroupBox1.Controls.Add(Me.txtAssembly)
      Me.GroupBox1.Controls.Add(Me.Label1)
      Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
      Me.GroupBox1.Name = "GroupBox1"
      Me.GroupBox1.Size = New System.Drawing.Size(600, 168)
      Me.GroupBox1.TabIndex = 0
      Me.GroupBox1.TabStop = False
      Me.GroupBox1.Text = "Referenced assemblies"
      '
      'lstAssemblies
      '
      Me.lstAssemblies.Location = New System.Drawing.Point(16, 56)
      Me.lstAssemblies.Name = "lstAssemblies"
      Me.lstAssemblies.Size = New System.Drawing.Size(568, 95)
      Me.lstAssemblies.TabIndex = 3
      '
      'btnAssemblyAdd
      '
      Me.btnAssemblyAdd.Location = New System.Drawing.Point(520, 24)
      Me.btnAssemblyAdd.Name = "btnAssemblyAdd"
      Me.btnAssemblyAdd.Size = New System.Drawing.Size(64, 23)
      Me.btnAssemblyAdd.TabIndex = 2
      Me.btnAssemblyAdd.Text = "Add"
      '
      'txtAssembly
      '
      Me.txtAssembly.Location = New System.Drawing.Point(88, 24)
      Me.txtAssembly.Name = "txtAssembly"
      Me.txtAssembly.Size = New System.Drawing.Size(424, 20)
      Me.txtAssembly.TabIndex = 1
      Me.txtAssembly.Text = ""
      '
      'Label1
      '
      Me.Label1.Location = New System.Drawing.Point(8, 24)
      Me.Label1.Name = "Label1"
      Me.Label1.Size = New System.Drawing.Size(80, 23)
      Me.Label1.TabIndex = 0
      Me.Label1.Text = "Assembly path"
      '
      'GroupBox2
      '
      Me.GroupBox2.Controls.Add(Me.txtCompilerOptions)
      Me.GroupBox2.Controls.Add(Me.Label6)
      Me.GroupBox2.Controls.Add(Me.txtResource)
      Me.GroupBox2.Controls.Add(Me.Label5)
      Me.GroupBox2.Controls.Add(Me.txtWarningLevel)
      Me.GroupBox2.Controls.Add(Me.Label4)
      Me.GroupBox2.Controls.Add(Me.chkErrors)
      Me.GroupBox2.Controls.Add(Me.txtOutput)
      Me.GroupBox2.Controls.Add(Me.Label3)
      Me.GroupBox2.Controls.Add(Me.txtMainClass)
      Me.GroupBox2.Controls.Add(Me.Label2)
      Me.GroupBox2.Controls.Add(Me.chkDebug)
      Me.GroupBox2.Controls.Add(Me.chkInMemory)
      Me.GroupBox2.Controls.Add(Me.chkExe)
      Me.GroupBox2.Location = New System.Drawing.Point(8, 184)
      Me.GroupBox2.Name = "GroupBox2"
      Me.GroupBox2.Size = New System.Drawing.Size(600, 192)
      Me.GroupBox2.TabIndex = 1
      Me.GroupBox2.TabStop = False
      Me.GroupBox2.Text = "Compiler Options"
      '
      'txtCompilerOptions
      '
      Me.txtCompilerOptions.Location = New System.Drawing.Point(336, 152)
      Me.txtCompilerOptions.Name = "txtCompilerOptions"
      Me.txtCompilerOptions.Size = New System.Drawing.Size(144, 20)
      Me.txtCompilerOptions.TabIndex = 14
      Me.txtCompilerOptions.Text = ""
      '
      'Label6
      '
      Me.Label6.Location = New System.Drawing.Point(208, 152)
      Me.Label6.Name = "Label6"
      Me.Label6.Size = New System.Drawing.Size(120, 23)
      Me.Label6.TabIndex = 13
      Me.Label6.Text = "Compiler options"
      '
      'txtResource
      '
      Me.txtResource.Location = New System.Drawing.Point(336, 120)
      Me.txtResource.Name = "txtResource"
      Me.txtResource.Size = New System.Drawing.Size(144, 20)
      Me.txtResource.TabIndex = 12
      Me.txtResource.Text = ""
      '
      'Label5
      '
      Me.Label5.Location = New System.Drawing.Point(208, 120)
      Me.Label5.Name = "Label5"
      Me.Label5.Size = New System.Drawing.Size(120, 23)
      Me.Label5.TabIndex = 11
      Me.Label5.Text = "Win32 resource file"
      '
      'txtWarningLevel
      '
      Me.txtWarningLevel.Location = New System.Drawing.Point(336, 88)
      Me.txtWarningLevel.Name = "txtWarningLevel"
      Me.txtWarningLevel.Size = New System.Drawing.Size(144, 20)
      Me.txtWarningLevel.TabIndex = 10
      Me.txtWarningLevel.Text = ""
      '
      'Label4
      '
      Me.Label4.Location = New System.Drawing.Point(208, 88)
      Me.Label4.Name = "Label4"
      Me.Label4.Size = New System.Drawing.Size(120, 23)
      Me.Label4.TabIndex = 9
      Me.Label4.Text = "Warning level"
      '
      'chkErrors
      '
      Me.chkErrors.Location = New System.Drawing.Point(16, 120)
      Me.chkErrors.Name = "chkErrors"
      Me.chkErrors.Size = New System.Drawing.Size(168, 24)
      Me.chkErrors.TabIndex = 8
      Me.chkErrors.Text = "Treat warnings as errors"
      '
      'txtOutput
      '
      Me.txtOutput.Location = New System.Drawing.Point(336, 56)
      Me.txtOutput.Name = "txtOutput"
      Me.txtOutput.Size = New System.Drawing.Size(144, 20)
      Me.txtOutput.TabIndex = 7
      Me.txtOutput.Text = ""
      '
      'Label3
      '
      Me.Label3.Location = New System.Drawing.Point(208, 56)
      Me.Label3.Name = "Label3"
      Me.Label3.Size = New System.Drawing.Size(120, 23)
      Me.Label3.TabIndex = 6
      Me.Label3.Text = "Output assembly name"
      '
      'txtMainClass
      '
      Me.txtMainClass.Location = New System.Drawing.Point(336, 24)
      Me.txtMainClass.Name = "txtMainClass"
      Me.txtMainClass.Size = New System.Drawing.Size(144, 20)
      Me.txtMainClass.TabIndex = 5
      Me.txtMainClass.Text = ""
      '
      'Label2
      '
      Me.Label2.Location = New System.Drawing.Point(208, 24)
      Me.Label2.Name = "Label2"
      Me.Label2.Size = New System.Drawing.Size(72, 23)
      Me.Label2.TabIndex = 4
      Me.Label2.Text = "Main class"
      '
      'chkDebug
      '
      Me.chkDebug.Location = New System.Drawing.Point(16, 88)
      Me.chkDebug.Name = "chkDebug"
      Me.chkDebug.Size = New System.Drawing.Size(168, 24)
      Me.chkDebug.TabIndex = 3
      Me.chkDebug.Text = "Include debug information"
      '
      'chkInMemory
      '
      Me.chkInMemory.Location = New System.Drawing.Point(16, 56)
      Me.chkInMemory.Name = "chkInMemory"
      Me.chkInMemory.Size = New System.Drawing.Size(136, 24)
      Me.chkInMemory.TabIndex = 2
      Me.chkInMemory.Text = "Generate in memory"
      '
      'chkExe
      '
      Me.chkExe.Location = New System.Drawing.Point(16, 24)
      Me.chkExe.Name = "chkExe"
      Me.chkExe.Size = New System.Drawing.Size(136, 24)
      Me.chkExe.TabIndex = 1
      Me.chkExe.Text = "Generate executable"
      '
      'GroupBox3
      '
      Me.GroupBox3.Controls.Add(Me.lstCodeFiles)
      Me.GroupBox3.Controls.Add(Me.btnCodeAdd)
      Me.GroupBox3.Controls.Add(Me.txtCodeToAdd)
      Me.GroupBox3.Controls.Add(Me.Label7)
      Me.GroupBox3.Location = New System.Drawing.Point(8, 384)
      Me.GroupBox3.Name = "GroupBox3"
      Me.GroupBox3.Size = New System.Drawing.Size(600, 168)
      Me.GroupBox3.TabIndex = 0
      Me.GroupBox3.TabStop = False
      Me.GroupBox3.Text = "Code files"
      '
      'lstCodeFiles
      '
      Me.lstCodeFiles.Location = New System.Drawing.Point(16, 56)
      Me.lstCodeFiles.Name = "lstCodeFiles"
      Me.lstCodeFiles.Size = New System.Drawing.Size(568, 95)
      Me.lstCodeFiles.TabIndex = 3
      '
      'btnCodeAdd
      '
      Me.btnCodeAdd.Location = New System.Drawing.Point(520, 24)
      Me.btnCodeAdd.Name = "btnCodeAdd"
      Me.btnCodeAdd.Size = New System.Drawing.Size(64, 23)
      Me.btnCodeAdd.TabIndex = 2
      Me.btnCodeAdd.Text = "Add"
      '
      'txtCodeToAdd
      '
      Me.txtCodeToAdd.Location = New System.Drawing.Point(88, 24)
      Me.txtCodeToAdd.Name = "txtCodeToAdd"
      Me.txtCodeToAdd.Size = New System.Drawing.Size(424, 20)
      Me.txtCodeToAdd.TabIndex = 1
      Me.txtCodeToAdd.Text = ""
      '
      'Label7
      '
      Me.Label7.Location = New System.Drawing.Point(8, 24)
      Me.Label7.Name = "Label7"
      Me.Label7.Size = New System.Drawing.Size(80, 23)
      Me.Label7.TabIndex = 0
      Me.Label7.Text = "Code file path"
      '
      'btnCompile
      '
      Me.btnCompile.Location = New System.Drawing.Point(536, 568)
      Me.btnCompile.Name = "btnCompile"
      Me.btnCompile.Size = New System.Drawing.Size(64, 23)
      Me.btnCompile.TabIndex = 4
      Me.btnCompile.Text = "Compile"
      '
      'Form1
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(640, 612)
      Me.Controls.Add(Me.GroupBox2)
      Me.Controls.Add(Me.GroupBox1)
      Me.Controls.Add(Me.GroupBox3)
      Me.Controls.Add(Me.btnCompile)
      Me.Name = "Form1"
      Me.Text = "Compiler Test"
      Me.GroupBox1.ResumeLayout(False)
      Me.GroupBox2.ResumeLayout(False)
      Me.GroupBox3.ResumeLayout(False)
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private mVBNetCompiler As New VBNetCompiler

   Private Sub btnAssemblyAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssemblyAdd.Click
      lstAssemblies.Items.Add(txtAssembly.Text)
      txtAssembly.Text = ""
   End Sub

   Private Sub btnCodeAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCodeAdd.Click
      lstCodeFiles.Items.Add(txtCodeToAdd.Text)
      txtCodeToAdd.Text = ""
   End Sub

   Private Sub btnCompile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompile.Click
      With mVBNetCompiler.VBCompilerParameters
         .GenerateExecutable = chkExe.Checked
         .GenerateInMemory = chkInMemory.Checked
         .IncludeDebugInformation = chkDebug.Checked
         .MainClass = txtMainClass.Text
         .OutputAssembly = txtOutput.Text
         .TreatWarningsAsErrors = chkErrors.Checked

         If txtWarningLevel.Text <> "" AndAlso IsNumeric(txtWarningLevel.Text) Then
            .WarningLevel = CInt(txtWarningLevel.Text)
         End If

         If txtResource.Text <> "" AndAlso IsNumeric(txtResource.Text) Then
            .Win32Resource = txtResource.Text
         End If
         If txtCompilerOptions.Text <> "" AndAlso IsNumeric(txtCompilerOptions.Text) Then
            .CompilerOptions = txtCompilerOptions.Text
         End If
      End With

      With mVBNetCompiler.ImportedDlls
         .Clear()
         For Each oElement As String In lstAssemblies.Items
            .Add(oElement)
         Next
      End With

      With mVBNetCompiler.FilesToCompile
         .Clear()
         For Each oElement As String In lstCodeFiles.Items
            .Add(oElement)
         Next
      End With

      mVBNetCompiler.Compile()
   End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Argentina Argentina
Gustavo is Information Systems Enginner and
Information Systems Analyst. He born in Colonia Caroya, Cordoba, Argentine.
He worked in the Universidad Tecnologica Nacional in Cordoba, Argentine like Research Group Coordinator in the Business Intelligence area.

After that worked like a professor of Visual Basic, SQL Server 2000, .NET Plataform, Visual Basic.NET and C#

His experience in programming includes: QuickBasic, C, C++, FoxPro, Visual Basic, VB.NET, C# and ASP.NET. He has working with XML, XSD, XSLT and other related technologies.

He is now working in Pectra Technology, in the development and the software engineering area in a product named Pectra BPM Studio Framework (Business Process Management Solution) using Microsoft.NET and XML technologies.

His hobbies are play the piano and reading. He is a science lover

Comments and Discussions