Difference between revisions of "Pascal for Visual Basic users"

From Lazarus wiki
m (Using default parameters in functions and procedures: A comment requires two /)
(Using default parameters in functions and procedures: clarify a bit)
Line 68: Line 68:
  
 
== Using default parameters in functions and procedures ==
 
== Using default parameters in functions and procedures ==
In Pascal it is possible to use automatically the default values of functions and procedures, only if they are not followed by any other parameters.
+
In Pascal it is possible to use automatically the default values of functions and procedures, only if they are not followed by other parameters that do not have a default value.
  
 
Example:
 
Example:
Line 83: Line 83:
 
will result in an error.
 
will result in an error.
  
It can only be called like this:
+
When calling the procedure or function, if you specify a paramter which has a default value, you must also specify all paramters that have a default value that are in front of it.<br>
 +
So, in this example if you want to have the third parameter being True, then you must also specify the value of second parameter.
 +
 
 +
These are valid calls:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
   SampleProc(5,'something',False);
 
   SampleProc(5,'something',False);
 
</syntaxhighlight>
 
</syntaxhighlight>
The next usage is also valid in Pascal:
+
 
 
<syntaxhighlight>
 
<syntaxhighlight>
 
   SampleProc(5,'nothing');
 
   SampleProc(5,'nothing');
 +
</syntaxhighlight>
 +
 +
<syntaxhighlight>
 +
  SampleProc(5);
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 16:00, 13 February 2015

Template:Pascal for VisualBasic users

Overview

This wiki page is created to help you switch from VisualBasic to Lazarus (FreePascal). It is not meant to convince that Lazarus is better than VB or vice versa. Anyway, these two languages have a lot of differences. Since in the beginning you would face problems with the things, that Lazarus cannot do and VB can, more attention is given to them.

Starting and ending statements

Pascal has no separate start and end statements for functions, procedures, loops, etc. It has only start and end. This problem can be easily solved, by manually adding a comment on end statements. Example:

for i:= 0 to 100 do
begin
   ...
end; //for i

Variables

Declaring variables

That is why *all* variables have to be declared in FreePascal. There is another restriction- variables are declared only in a special section. Declaration in the code is not possible. Example:

procedure VarDecl;
var
  MyVar1: string;
begin
  writeln ('Print something')
  MyVar1: string; //This is not possible!!
  writeln ('Print something else')
end; //VarDecl

Types of variables

Besides the variables known in VB, FreePascal supports unsigned integers. You should pay special attention not to mix them with signed integers.

Loops

FreePascal has three kind of loops.

for... do...

while... do

repeat... until

For... do...

This is quite similar to the For.. Next loop in VB, but not as much powerful, due to the following limitations:

1. A counter cannot be float number.

2. There is no Step property. To decrease the number of the counter, DOWNTO shall be used instead of TO.

Example

procedure ForLoop;
var
  i: integer;
begin
  for i:=50 downto 0 do
  begin
     ...
  end. //for i
end. //func

3. Value of the counter cannot be changed inside the loop.

Using default parameters in functions and procedures

In Pascal it is possible to use automatically the default values of functions and procedures, only if they are not followed by other parameters that do not have a default value.

Example: If a procedure is declared the following way

procedure SampleProc(parm1: integer; parm2: string= 'something'; parm3:Boolean= True);
begin
end. //proc

it is not possible to call it as in VB and

  SampleProc(5,,False);

will result in an error.

When calling the procedure or function, if you specify a paramter which has a default value, you must also specify all paramters that have a default value that are in front of it.
So, in this example if you want to have the third parameter being True, then you must also specify the value of second parameter.

These are valid calls:

  SampleProc(5,'something',False);
  SampleProc(5,'nothing');
  SampleProc(5);