Customer has some example code written in Visual Basic .NET and he would like to see a comparison of how bthe same code would look in Visual COBOL.
The Visual Basic example looks like:
Imports System Imports System.Text Imports System.IO Imports System.Net.Mail
Module Module1 Dim mail As System.Net.Mail.MailMessage Dim wshost As String = Nothing Dim wsport As String = Nothing Dim wssender As String = Nothing Dim wspassword As String = Nothing Dim wsrecipient As String = Nothing Dim wssubject As String = Nothing Dim wsbody As String = Nothing Dim wsfile As String = Nothing Dim wiport As Integer Dim StreamEncoding As Encoding Dim txtline As String = Nothing Dim txtline2() As String = Nothing Dim flag As Integer
Public Sub Main() 'Before read and write getting encode (for Turkish) StreamEncoding = System.Text.Encoding.GetEncoding("iso-8859-9") Dim fs1 As FileStream fs1 = New FileStream("C:\inform.txt", FileMode.Open, FileAccess.Read) Dim freader As New IO.StreamReader(fs1, StreamEncoding)
'Write exceptions information to the file Dim fs2 As FileStream fs2 = New FileStream("C:\result.txt", FileMode.Create, FileAccess.Write) Dim fwriter As New IO.StreamWriter(fs2, StreamEncoding)
Do Until freader.EndOfStream()
'Reading file (txt) line by line and truncating
txtline = freader.ReadLine If txtline = "" Then Exit Do txtline2 = txtline.Split("»") wshost = txtline2(0) wsport = txtline2(1) wssender = txtline2(2) wspassword = txtline2(3) wsrecipient = txtline2(4) wssubject = txtline2(5) wsbody = txtline2(6) wsfile = txtline2(7) wiport = CInt(wsport) Dim Mail As New MailMessage Mail.From = New MailAddress(wssender) Mail.To.Add(wsrecipient) Mail.Subject = wssubject Mail.IsBodyHtml = True Mail.Body = wsbody Dim smtp As New SmtpClient(wshost) smtp.EnableSsl = True smtp.Port = wiport smtp.UseDefaultCredentials = False smtp.Credentials = New System.Net.NetworkCredential(wssender, wspassword) If Not wsfile.Equals(String.Empty) Then Mail.Attachments.Add(New Attachment(wsfile)) End If Try smtp.Send(Mail) fwriter.WriteLine("1" & wsrecipient)
Catch ex As Net.Mail.SmtpException fwriter.WriteLine("0" & wsrecipient)
End Try Mail.Dispose() 'Disposing Loop freader.Close() fs1.Close() fwriter.Close() fs2.Close()
End Sub End Module
Visual COBOL provides for full access to the .NET Framework classes just like you have in VB or in C#. In fact, the syntax looks very similar as can be seen below.
This example was created using Visual COBOL 2.0 and demonstrates the new feature of allowing local variables to be defined in the procedure division using the DECLARE statement or directly in a PERFORM or CATCH statement. It also uses the "" to indicate 0 based arrays.
$set ilusing"System" $set ilusing"System.Text" $set ilusing"System.IO" $set ilusing"System.Net.Mail" program-id. Program1 as "testemail.Program1". data division. working-storage section. 01 mail type System.Net.Mail.MailMessage. 01 wshost string value "". 01 wsport string value "". 01 wssender string value "". 01 wspassword string value "". 01 wsrecipient string value "". 01 wssubject string value "". 01 wsbody string value "". 01 wsfile string value "". 01 wiport binary-long. 01 StreamEncoding type Encoding. 01 txtline string value "". 01 txtline2 string occurs any. 01 flag binary-long. procedure division. *>Before read and write getting encode (for Turkish) set StreamEncoding to type System.Text.Encoding::GetEncoding("iso-8859-9") declare fs1 as type FileStream = new FileStream("C:\inform.txt", type FileMode::Open, type FileAccess::Read) declare freader as type StreamReader = new StreamReader(fs1, StreamEncoding) *>Write exceptions information to the file declare fs2 as type FileStream = new type FileStream("C:\result.txt", type FileMode::Create, type FileAccess::Write) declare fwriter as type StreamWriter = new type StreamWriter(fs2, StreamEncoding) perform until freader::EndOfStream *>Reading file (txt) line by line and truncating set txtline to freader::ReadLine if txtline = "" exit perform end-if set txtline2 to txtline::Split("»") set wshost to txtline2 set wsport to txtline2 set wssender to txtline2 set wspassword to txtline2 set wsrecipient to txtline2 set wssubject to txtline2 set wsbody to txtline2 set wsfile to txtline2 set wiport to type System.Convert::ToInt32(wsport) set mail to new MailMessage set mail::From to new MailAddress(wssender) invoke mail::To::Add(wsrecipient) set mail::Subject to wssubject set mail::IsBodyHtml to True set mail::Body to wsbody declare smtp as type SmtpClient = new SmtpClient(wshost) set smtp::EnableSsl to True set smtp::Port to wiport set smtp::UseDefaultCredentials to False set smtp::Credentials to new type System.Net.NetworkCredential(wssender, wspassword) if not wsfile::Equals(type String::Empty) invoke mail::Attachments::Add(New Attachment(wsfile)) end-if try invoke smtp::Send(Mail) invoke fwriter::WriteLine("1" & wsrecipient) catch ex as type SmtpException invoke fwriter::WriteLine("0" & wsrecipient) end-try invoke mail::Dispose end-perform *>Disposing invoke freader::Close invoke fs1::Close invoke fwriter::Close invoke fs2::Close goback.
end program Program1.