Pascal for Visual Basic users
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
In Pascal all variables have to be declared before use, and you have to make the declarations in a special var section which must precede the code that makes use of the variable. You cannot declare a new variable in the middle of code.
procedure VarDecl; var MyVar1: string; begin WriteLn('Print something'); MyVar2: string = 'else'; // This declaration is not allowed here - it must be moved three lines above to the var section WriteLn('Print something ', MyVar2); 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.
FreePascal has three kind of loops.
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.
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 functions and procedures it is possible for parameters to automatically take default values. However, in the parameter list the parameters that might take default values must be declared as a contiguous list at the end of the parameter declaration part. Also, when a routine with default parameter(s) is subsequently called in your code, you can only specify non-default values beginning at the first parameter which would otherwise take a default value. You cannot specify a non-default value for a later parameter, if there are preceding parameters you have to specify values for which have not also been specified explicitly in the function or procedure call.
For example: If a procedure is declared (correctly) as follows with two default parameters,
procedure SampleProc(parm1: integer; parm2: string= 'something'; parm3:Boolean= True); begin end. //proc
it is not possible to call SampleProc as in VB like this:
This will produce a compiler error.
When calling a procedure (or function), if you specify a parameter which has a default value, you must also specify all parameters that have a default value that are in front of it. So, in the example above if you want the third parameter to be False, then you must also specify the value of second parameter.
These are valid calls:
SampleProc(5,'something',False); SampleProc(5,'nothing'); SampleProc(5,''); SampleProc(5);