Project: COBOL in .NET

Filed under: Code Comments: 0

For your viewing pleasure, a sample application I had to build for course I attended a while ago. We wrote some sample applications in a variety of languages, and all of them were implemented within the .NET Framework.

This application demonstrates COBOL in .NET.

Program.cbl

   1:         class-id. Main as "SENG.COBOL.App.Main".
   2:         environment division.
   3:         configuration section.
   4:         repository.
   5:        
   6:         static.
   7:    
   8:         method-id. "Main" 
   9:             custom-attribute is type "System.STAThreadAttribute".
  10:         local-storage section.
  11:         01 mainForm type "SENG.COBOL.App.Form1".
  12:         procedure division.
  13:         
  14:             set mainForm to new "SENG.COBOL.App.Form1"()
  15:             invoke type "System.Windows.Forms.Application"::"Run"(mainForm)
  16:             goback.
  17:         
  18:         end method "Main".
  19:    
  20:         end static.
  21:         end class Main.

This part of the code is the entry point for the CLR.

Form1.cbl

   1:         class-id. Form1 as "SENG.COBOL.App.Form1" is partial
   2:                   inherits type "System.Windows.Forms.Form".
   3:                   
   4:         environment division.
   5:         configuration section.
   6:         repository.
   7:        
   8:         object.
   9:         working-storage section.
  10:         01 FirstName    PIC A(25).
  11:         01 LastName     PIC A(75).
  12:         01 PayRate      PIC 9(10)V9(2).
  13:         01 HoursWorked  PIC 9(4)V9(2).
  14:         01 GrossPay     PIC 9(14)V9(2).
  15:         
  16:         method-id. NEW.
  17:         procedure division.
  18:             invoke self::"InitializeComponent"
  19:             goback.
  20:         end method NEW.
  21:   
  22:         method-id.  "btnCalc_Click" final private.
  23:         procedure division using by value sender as object e as type "System.EventArgs".
  24:             SET FirstName TO TYPE "System.Convert"::"ToString"(self::"txtFirst"::"Text").
  25:             SET LastName TO TYPE "System.Convert"::"ToString"(self::"txtLast"::"Text").
  26:             SET PayRate TO type "System.Convert"::"ToDecimal"(self::"txtRate"::"Text").
  27:             SET HoursWorked TO TYPE "System.Convert"::"ToDecimal"(self::"txtHours"::"Text").
  28:             MULTIPLY PayRate BY HoursWorked GIVING GrossPay.
  29:             SET self::"lblPayStub"::"Text" TO TYPE "System.Convert"::"ToString"(GrossPay).
  30:         end method "btnCalc_Click".
  31:   
  32:         method-id.  "btnClear_Click" final private.
  33:         procedure division using by value sender as object e as type "System.EventArgs".
  34:             SET self::"txtFirst"::"Text" TO NULL.
  35:             SET self::"txtLast"::"Text" TO NULL.
  36:             SET self::"txtRate"::"Text" TO NULL.
  37:             SET self::"txtHours"::"Text" TO NULL.
  38:             SET self::"lblPayStub"::"Text" TO NULL.
  39:         end method "btnClear_Click".
  40:   
  41:         method-id.  "btnExit_Click" final private.
  42:         procedure division using by value sender as object e as type "System.EventArgs".
  43:             STOP RUN.
  44:         end method "btnExit_Click".
  45:        
  46:         end object.
  47:         end class Form1.

This is essentially the application logic. It specifies the event handlers for the controls used by this project.

Form1.Designer.cbl

   1:         class-id. Form1 as "SENG.COBOL.App.Form1" is partial
   2:                   inherits type "System.Windows.Forms.Form".
   3:                   
   4:         environment division.
   5:         configuration section.
   6:         repository.
   7:        
   8:         object.
   9:         working-storage section.
  10:         01 label1 type "System.Windows.Forms.Label".
  11:         01 label2 type "System.Windows.Forms.Label".
  12:         01 txtFirst type "System.Windows.Forms.TextBox".
  13:         01 txtLast type "System.Windows.Forms.TextBox".
  14:         01 label3 type "System.Windows.Forms.Label".
  15:         01 txtRate type "System.Windows.Forms.TextBox".
  16:         01 label4 type "System.Windows.Forms.Label".
  17:         01 txtHours type "System.Windows.Forms.TextBox".
  18:         01 label5 type "System.Windows.Forms.Label".
  19:         01 btnCalc type "System.Windows.Forms.Button".
  20:         01 btnClear type "System.Windows.Forms.Button".
  21:         01 btnExit type "System.Windows.Forms.Button".
  22:         01 lblPayStub type "System.Windows.Forms.Label".
  23:         01 components type "System.ComponentModel.IContainer".
  24:        
  25:        *> Required method for Designer support - do not modify
  26:        *> the contents of this method with the code editor.
  27:         method-id.  "InitializeComponent" private.
  28:         procedure division.
  29:         set txtFirst to new "System.Windows.Forms.TextBox"
  30:         set label1 to new "System.Windows.Forms.Label"
  31:         set label2 to new "System.Windows.Forms.Label"
  32:         set txtLast to new "System.Windows.Forms.TextBox"
  33:         set label3 to new "System.Windows.Forms.Label"
  34:         set txtRate to new "System.Windows.Forms.TextBox"
  35:         set label4 to new "System.Windows.Forms.Label"
  36:         set txtHours to new "System.Windows.Forms.TextBox"
  37:         set label5 to new "System.Windows.Forms.Label"
  38:         set btnCalc to new "System.Windows.Forms.Button"
  39:         set btnClear to new "System.Windows.Forms.Button"
  40:         set btnExit to new "System.Windows.Forms.Button"
  41:         set lblPayStub to new "System.Windows.Forms.Label"
  42:         invoke self::"SuspendLayout"
  43:        *> 
  44:        *> txtFirst
  45:        *> 
  46:         set txtFirst::"Location" to new "System.Drawing.Point"( 27 60)
  47:         set txtFirst::"Name" to "txtFirst"
  48:         set txtFirst::"Size" to new "System.Drawing.Size"( 244 23)
  49:         set txtFirst::"TabIndex" to 0
  50:        *> 
  51:        *> label1
  52:        *> 
  53:         set label1::"AutoSize" to True
  54:         set label1::"Location" to new "System.Drawing.Point"( 24 40)
  55:         set label1::"Name" to "label1"
  56:         set label1::"Size" to new "System.Drawing.Size"( 79 17)
  57:         set label1::"TabIndex" to 1
  58:         set label1::"Text" to "First Name:"
  59:        *> 
  60:        *> label2
  61:        *> 
  62:         set label2::"AutoSize" to True
  63:         set label2::"Location" to new "System.Drawing.Point"( 24 100)
  64:         set label2::"Name" to "label2"
  65:         set label2::"Size" to new "System.Drawing.Size"( 81 17)
  66:         set label2::"TabIndex" to 3
  67:         set label2::"Text" to "Last Name:"
  68:        *> 
  69:        *> txtLast
  70:        *> 
  71:         set txtLast::"Location" to new "System.Drawing.Point"( 27 120)
  72:         set txtLast::"Name" to "txtLast"
  73:         set txtLast::"Size" to new "System.Drawing.Size"( 244 23)
  74:         set txtLast::"TabIndex" to 2
  75:        *> 
  76:        *> label3
  77:        *> 
  78:         set label3::"AutoSize" to True
  79:         set label3::"Location" to new "System.Drawing.Point"( 26 163)
  80:         set label3::"Name" to "label3"
  81:         set label3::"Size" to new "System.Drawing.Size"( 69 17)
  82:         set label3::"TabIndex" to 5
  83:         set label3::"Text" to "Pay Rate:"
  84:        *> 
  85:        *> txtRate
  86:        *> 
  87:         set txtRate::"Location" to new "System.Drawing.Point"( 29 183)
  88:         set txtRate::"Name" to "txtRate"
  89:         set txtRate::"Size" to new "System.Drawing.Size"( 108 23)
  90:         set txtRate::"TabIndex" to 4
  91:        *> 
  92:        *> label4
  93:        *> 
  94:         set label4::"AutoSize" to True
  95:         set label4::"Location" to new "System.Drawing.Point"( 160 163)
  96:         set label4::"Name" to "label4"
  97:         set label4::"Size" to new "System.Drawing.Size"( 101 17)
  98:         set label4::"TabIndex" to 7
  99:         set label4::"Text" to "Hours Worked:"
 100:        *> 
 101:        *> txtHours
 102:        *> 
 103:         set txtHours::"Location" to new "System.Drawing.Point"( 163 183)
 104:         set txtHours::"Name" to "txtHours"
 105:         set txtHours::"Size" to new "System.Drawing.Size"( 108 23)
 106:         set txtHours::"TabIndex" to 6
 107:        *> 
 108:        *> label5
 109:        *> 
 110:         set label5::"AutoSize" to True
 111:         set label5::"Location" to new "System.Drawing.Point"( 26 232)
 112:         set label5::"Name" to "label5"
 113:         set label5::"Size" to new "System.Drawing.Size"( 67 17)
 114:         set label5::"TabIndex" to 9
 115:         set label5::"Text" to "Pay Stub:"
 116:        *> 
 117:        *> btnCalc
 118:        *> 
 119:         set btnCalc::"Location" to new "System.Drawing.Point"( 287 252)
 120:         set btnCalc::"Name" to "btnCalc"
 121:         set btnCalc::"Size" to new "System.Drawing.Size"( 75 23)
 122:         set btnCalc::"TabIndex" to 10
 123:         set btnCalc::"Text" to "Calc"
 124:         set btnCalc::"UseVisualStyleBackColor" to True
 125:         invoke btnCalc::"add_Click"(new "System.EventHandler"(self::"btnCalc_Click"))
 126:        *> 
 127:        *> btnClear
 128:        *> 
 129:         set btnClear::"Location" to new "System.Drawing.Point"( 287 281)
 130:         set btnClear::"Name" to "btnClear"
 131:         set btnClear::"Size" to new "System.Drawing.Size"( 75 23)
 132:         set btnClear::"TabIndex" to 11
 133:         set btnClear::"Text" to "Clear"
 134:         set btnClear::"UseVisualStyleBackColor" to True
 135:         invoke btnClear::"add_Click"(new "System.EventHandler"(self::"btnClear_Click"))
 136:        *> 
 137:        *> btnExit
 138:        *> 
 139:         set btnExit::"Location" to new "System.Drawing.Point"( 287 317)
 140:         set btnExit::"Name" to "btnExit"
 141:         set btnExit::"Size" to new "System.Drawing.Size"( 75 23)
 142:         set btnExit::"TabIndex" to 12
 143:         set btnExit::"Text" to "Exit"
 144:         set btnExit::"UseVisualStyleBackColor" to True
 145:         invoke btnExit::"add_Click"(new "System.EventHandler"(self::"btnExit_Click"))
 146:        *> 
 147:        *> lblPayStub
 148:        *> 
 149:         set lblPayStub::"Font" to new "System.Drawing.Font"( "Century Gothic" 14.25 type "System.Drawing.FontStyle"::"Regular" type "System.Drawing.GraphicsUnit"::"Point" 0 as type "System.Byte")
 150:         set lblPayStub::"Location" to new "System.Drawing.Point"( 26 255)
 151:         set lblPayStub::"Name" to "lblPayStub"
 152:         set lblPayStub::"Size" to new "System.Drawing.Size"( 245 85)
 153:         set lblPayStub::"TabIndex" to 13
 154:        *> 
 155:        *> Form1
 156:        *> 
 157:         set self::"ClientSize" to new "System.Drawing.Size"( 384 364)
 158:         invoke self::"Controls"::"Add"(lblPayStub)
 159:         invoke self::"Controls"::"Add"(btnExit)
 160:         invoke self::"Controls"::"Add"(btnClear)
 161:         invoke self::"Controls"::"Add"(btnCalc)
 162:         invoke self::"Controls"::"Add"(label5)
 163:         invoke self::"Controls"::"Add"(label4)
 164:         invoke self::"Controls"::"Add"(txtHours)
 165:         invoke self::"Controls"::"Add"(label3)
 166:         invoke self::"Controls"::"Add"(txtRate)
 167:         invoke self::"Controls"::"Add"(label2)
 168:         invoke self::"Controls"::"Add"(txtLast)
 169:         invoke self::"Controls"::"Add"(label1)
 170:         invoke self::"Controls"::"Add"(txtFirst)
 171:         set self::"Font" to new "System.Drawing.Font"( "Century Gothic" 9.75 type "System.Drawing.FontStyle"::"Regular" type "System.Drawing.GraphicsUnit"::"Point" 0 as type "System.Byte")
 172:         set self::"MaximizeBox" to False
 173:         set self::"Name" to "Form1"
 174:         set self::"StartPosition" to type "System.Windows.Forms.FormStartPosition"::"CenterScreen"
 175:         set self::"Text" to "SENG.301 Project 1"
 176:         invoke self::"ResumeLayout"(False)
 177:         invoke self::"PerformLayout"
 178:         end method "InitializeComponent".
 179:   
 180:        *> Clean up any resources being used.      
 181:         method-id. "Dispose" override protected.
 182:         procedure division using by value disposing as condition-value.
 183:             if disposing then
 184:               if components not = null then
 185:                 invoke components::"Dispose"()
 186:               end-if
 187:             end-if
 188:             invoke super::"Dispose"(by value disposing)
 189:             goback.           
 190:         end method "Dispose".
 191:   
 192:         end object.
 193:         end class Form1.

This code draws the form for this application. Great stuff, eh?

As you can see, it's not pure COBOL, but that may be a good thing. The pure COBOL to create this form and run it would be pretty darn long and probably more prone to error.

Source Code

If you like, you may click this link to download the source code for this project.