Comparison Table

Language Feature Visual Basic Pascal
Statement separator : or new line (ASCII 13) ;
Comment

'Comment
Rem Comment

//Comment after code
{Comment spanning
lines}

Declaring constants Const VAT_RATE = 0.175 const VAT_RATE = 0.175;
Declaring variables


Dim Forename, Surname As String
Dim PaysTax As Boolean
Dim TotalHits As Integer
Dim AverageScore As Double

Dim Months (11) As String
  
Dim Rainfall(6, 11) As Integer
  
Dim AgeRecords(999) As TRec




Also Char (2 byte Unicode), Byte, Short, Single, Double, Decimal, Currency, Date ...

var
  Forename, Surname : string;
  PaysTax : Boolean;
  TotalHits : integer;
  AverageScore : double;

  (same as AverageScore : real;)
  Months : array [1..12] of
           string;
  Rainfall : array [2004 .. 2010,
              1..12] of integer;
  AgeRecords : array[1..1000] of
               TRec;

  
Also char (same as ansiChar), wideChar, byte, single, currency, TDateTime ...  

Assigning variables

Surname = "Morrison"
AverageScore = 24.56
DateSubmitted = #1/5/2007#

Months(1) = "January"
Rainfall(0, 1) = 15

Surname := 'Morrison';
AverageScore := 24.56;
DateSubmitted := StrToDateTime(
                  '1/5/2007');
Months[1] := 'January';
Rainfall[2000, 1] := 15;
or  
Rainfall[2000][1] := 15;

If statement
  One line
                  
                  
  More than one line


If CurrentNum > Max Then Max =
                   CurrentNum

                
If CurrentNum > Max Then
  Max = CurrentNum
  Console.WriteLine(
             "Max  increased")
End If


if CurrentNum > Max then
  Max := CurrentNum;
                
if CurrentNum > Max then
  begin
    Max := CurrentNum;
    writeln('Max increased');
  end;

Case statement

Select Case iChoice
  Case 1
    Maths()
  Case 2
    StringManipulation()
End Select

case iChoice of
  1 : Maths;
  2 : StringManipulation;
end;

For loop

For i = 1 To 10
  lines of code  
Next
  
  
For i = 10 To 1 Step -1
  lines of code
Next i

for i := 1 to 10 do
  begin
    lines of code
  end;
  
for i := 10 downto 1 do
  begin
    lines of code
  end;

Do/repeat loop

Do Until finished = True
  lines of code
Loop

repeat
  lines of code
until finished = true;

While loop

Do While Amount > 0
  lines of code  
Loop

while Amount > 0 do
  begin
    lines of code
  end;

Defining records

Structure TAgeRec
  <VBFixedString(15)> Dim Surname
                 As String
  Dim Age As Integer
End Structure

type
  TAgeRec = record
    Surname : String[15];
    Age : Integer;
  end;

Operators for:
  addition
  subtraction
  multiplication
  real division
  integer division
  remainder
  exponentiation
                


+
-
*
/
\
Mod
^


+
-
*
/
DIV
MOD

(Use the power function)

Procedure without parameters

Sub ListRecords()
  lines of code
End Sub

                  
                  
To call the procedure:
ListRecords()

procedure ListRecords;
begin
  lines of code
end;


To call the procedure:
ListRecords;

Procedure with parameter passed by value

Sub ListRec(ByVal RecNo As
            Integer)
  lines of code
End Sub

                  
                  
To call the procedure for the fifth record:
ListRec(5)

procedure ListRec(RecNo: Integer);
begin
  lines of code
end;


To call the procedure for the fifth record:
ListRec(5);

Procedure with parameter passed by reference

Sub Calc(ByRef Total As Integer)
  lines of code
End Sub

procedure Calc(var Total :
              integer);
begin
  lines of code
end;

Function

Function Cube(ByVal Num As Double)
         As Double
  Cube = Num ^ 3
End Function

function Cube(Num: double) :
         double;
begin
   Cube := Num * Num * Num;
end;

Inbuilt string functions
  Type conversions
                  
                  
  



  String manipulation


ToString() converts numeric value to string
                  
                  
                  
                  
CInt() converts expression to integer
                  

Len() returns length of string
                  
InStr() looks for one string within another
and returns the position of the first matching character.
                  
                  
Mid() returns the required number of characters
from any position in the string
Left() returns the required number of characters
from the front of the string
Right() returns the required number of characters
from the back of the string
                  
UCase()  returns a string in upper case
LCase() returns a string in lower case
                


IntToStr converts integer to string
FloatToStr converts real to string
FloatToStrf converts real to formatted string
DateTimeToStr converts from TDateTime to string
StrToInt converts string to integer  
                  


Length returns length of  string

Pos looks for one string within another
and returns the position of the first matching character.

(StrUtils Unit)
MidStr returns the required number of characters
from any position in the string
LeftStr returns the required number of characters
from the front of the string
RightStr returns the required number of characters
from the back of the string

UpperCase  returns a string in upper case
LowerCase returns a string in lower case

Writing a text file

Dim SW As New StreamWriter(
              "F:line.txt")
SW.WriteLine("One line only")
SW.Close()

var MyFile : TextFile;
begin
  assignFile (MyFile,
             'F:\line.txt');
  rewrite (MyFile);
  writeln('One line only');
  closeFile (MyFile);
end;

Reading a text file

Dim MyMessage As String
Dim SR As New StreamReader(
              "F:line.txt")
  MyMessage = SR.ReadLine()
  SR.Close

var
  MyMessage : String;
  MyFile : TextFile;
begin
  assignFile(MyFile,
             'F:\line.txt');
  reset(MyFile);
  readln(MyFile, MyMessage);
  closeFile(MyFile);
end;

We provide an alternative, neater but less colourful version of the same table. Without formatting, code lines need not be split and it should be more convenient for copying sections of code.

Language Feature Visual Basic Pascal
Statement separator : or new line (ASCII 13) ;
Comment

'Comment
Rem Comment

//Comment after code
{Comment spanning
lines}

Declaring constants Const VAT_RATE = 0.175 const VAT_RATE = 0.175;
Declaring variables


Forename, Surname As String
Dim PaysTax As Boolean
Dim TotalHits As Integer
Dim AverageScore As Double

Dim Months (11) As String
Dim Rainfall(6, 11) As Integer
Dim AgeRecords(999) As TRec

Also Char (2 byte Unicode), Byte, Short, Single,
Double, Decimal, Currency, Date ...

var
  Forename, Surname : String;
  PaysTax : Boolean;
  TotalHits : Integer;
  AverageScore : Double;
  (same as AverageScore : Real;)
  Months : array [1..12] of string;
  Rainfall : array [2005 .. 2010, 1..12] of Integer;
  AgeRecords : array[1..1000] of TRec;
  
Also Char (same as AnsiChar), WideChar, Byte,
Single, Double, Currency, TDateTime ...  

Assigning variables

Surname = "Morrison"
AverageScore = 24.56
DateSubmitted = #1/5/2007#
Months(1) = "January"
Rainfall(0, 1) = 15

Surname := 'Morrison';
AverageScore := 24.56;
DateSubmitted := StrToDateTime('1/5/2007');
Months[1] := 'January';
Rainfall[2000, 1] := 15;  or
Rainfall[2000][1] := 15;

Defining records

Structure TAgeRec
  <VBFixedString(15)> Dim Surname As String
  Dim Age As Integer
End Structure

type
  TAgeRec = record
    Surname : String[15];
    Age : Integer;
  end;

Writing a text file

Dim SW As New StreamWriter("F:\line.txt")
SW.WriteLine("One line only")
SW.Close()

var MyFile : TextFile;
begin
  assignFile (MyFile, 'F:\line.txt');
  rewrite (MyFile);
  writeln('One line only');
  closeFile (MyFile);
end;

Reading a text file

Dim MyMessage As String
Dim SR As New StreamReader("F:\line.txt")
MyMessage = SR.ReadLine()
SR.Close

var
  MyMessage : String;
  MyFile : TextFile;
begin
  assignFile(MyFile, 'F:\line.txt');
  reset(MyFile);
  readln(MyFile, MyMessage);
  closeFile(MyFile);
end;

Operators for:
  addition
  subtraction
  multiplication
  real division
  integer division
  remainder
  exponentiation e.g. a^b


+
-
*
/
\
Mod
^


+
-
*
/
DIV
MOD
(Use the power function)

Inbuilt string functions
Type conversions








String manipulation


ToString() converts numeric value to string
  
  
  
  
CInt converts expression to integer


Len returns length of string

InStr looks for one string within another
and returns the position of the first matching character.


Mid returns the required number of characters from
any position in the string
Left returns the required number of characters from
the front of the string
Right returns the required number of characters from
the back of the string

UCase  returns a string in upper case
LCase returns a string in lower case
              


IntToStr converts integer to string
FloatToStr converts real to string
FloatToStrf converts real to string and formats the result
DateTimeToStr converts from TDateTime to string
StrToInt converts string to integer


Length returns length of  string

Pos looks for one string within another
and returns the position of the first matching character.

(StrUtils Unit)
MidStr returns the required number of characters from
any position in the string
LeftStr  returns the required number of characters
from the front of the string
RightStr returns the required number of characters
from the back of the string

UpperCase  returns a string in upper case
LowerCase returns a string in lower case

If statement
  One line
              

  More than one line


If CurrentNum > Max Then Max = CurrentNum


If CurrentNum > Max Then
  Max = CurrentNum
  Console.WriteLine("Max increased")
End If


if CurrentNum > Max then
  Max := CurrentNum;

if CurrentNum > Max then
  begin
    Max := CurrentNum;
    writeln('Max increased');
  end;

Case statement

Select Case iChoice
  Case 1
    Maths()
  Case 2
    StringManipulation()
End Select

case iChoice of
  1 : Maths;
  2 : StringManipulation;
end;

For loop

For i = 1 To 10
  lines of code
Next


For i = 10 To 1 Step -1
   lines of code
  Next i

for i :=1 to 10 do
  begin
    lines of code
  end;
  
for i := 10 downto 1 do
  begin
    lines of code
  end;

Do/repeat loop

Do Until finished = True
  lines of code
Loop

repeat
  lines of code
until finished = true;

While loop

Do While Amount > 0
   lines of code
Loop

while Amount > 0 do
  begin
    lines of code
  end;

Procedure without parameters

Sub ListRecords()
   lines of code
End Sub


To call the procedure: ListRecords()

procedure ListRecords;
begin
   lines of code
end;

To call the procedure: ListRecords;

Procedure with parameter passed by value

Sub ListRec(ByVal RecNo As Integer)
   lines of code
End Sub


To call the procedure for the fifth record :
ListRec(5)

procedure ListRec(RecNo: Integer);
begin
   lines of code
end;

To call the procedure for the fifth record:
ListRec(5);

Procedure with parameter passed by reference

Sub Calc(ByRef Total As Integer)
   lines of code
End Sub

procedure Calc(var Total : Integer);
begin
   lines of code
  end;

Function

Function Cube(ByVal Num As Double) As Double
  Cube = Num ^ 3
End Function

function Cube(Num: Double) : Double;
begin
   Cube := Num * Num * Num;
end;

Programming - a skill for life!

Converting from Visual Basic to Pascal