Difference between revisions of "Pascal for Visual Basic users"
Line 36: | Line 36: | ||
Local variables do not get initialized upon declaration. They get random value (whatever value happen to be on the stack where they landed). Global variables are initialized to zero. | Local variables do not get initialized upon declaration. They get random value (whatever value happen to be on the stack where they landed). Global variables are initialized to zero. | ||
− | To initialize a local variable | + | To initialize a local variable upon declaration the following syntax is to be used: |
<syntaxhighlight> | <syntaxhighlight> | ||
var | var |
Revision as of 16:28, 18 February 2015
Template:Pascal for VisualBasic users
Overview
This wiki page has been created to help you if you are switching from VisualBasic to FreePascal (the Pascal dialect and compiler used by Lazarus). It is not meant to convince you that Lazarus is better than VB (or vice versa). Although VB and Pascal have superficial similarities these two languages differ greatly. Since VB programmers beginning to use Pascal face immediate difficulties in trying to do things which in VB have nice syntax that Pascal does not provide, we focus here on those differences.
Beginning and ending statements
Blocks of Pascal code have no distinguishing end statements for functions, procedures, loops, etc. Pascal needs only begin and end. You can easily add a comment manually following end statements to clarify which named routine corresponds to this particular end;.
For example:
for i:= 0 to 100 do
begin
...
end; // for i do
Variables
Declaring variables
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.
For example:
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
Local variables do not get initialized upon declaration. They get random value (whatever value happen to be on the stack where they landed). Global variables are initialized to zero.
To initialize a local variable upon declaration the following syntax is to be used:
var
MyVar1: string='';
MyVar2: integer='0';
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 four kinds of loops.
For... do...
This is quite similar to the For.. Next loop in VB, but more limited in syntax. The following restrictions apply in Pascal:
1. A for loop counter must be an integer (it cannot be a floating point type).
2. There is no Step property. To emulate the VB Step syntax you have to introduce a second counter variable yourself.
3. To decrement the value of the loop counter from a positive value, you use the keyword DOWNTO in place of TO.
For example:
procedure ForLoop;
var
i: integer;
begin
for i:=50 downto 0 do
begin
...
end. //for i
end. // procedure ForLoop
3. The value of the counter (i in the example above) cannot be changed programmatically inside the loop. Attempting to do so will throw a compiler error.
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:
SampleProc(5,,False);
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);
Dynamic arrays
In Pascal the minimum index of a dynamic array is 0, i.e. MyArray (1) will return the second elements of an array named MyArray.
Arrays are resized using
setlength (MyArray, NewDimension);
SetLength does not clear the array, i.e. it acts like ReDim Preserve, not like ReDim.
Translation of some commonly sought commands
DoEvents = TApplication.ProcessMessages
VB commands, which are not present in fpc/Lazarus
Split - fpc and Lazarus have no native function to split strings. TStrings could be used instead of a string array, in case that the string separator is a character.
Join - fpc and Lazarus have no native function to join strings.
Debugging
Lazarus IDE has no Instant panel.
In order to debug in Windows the dos prompt windows shall be enabled the following way: Lazarus IDE-> Main menu -> Project -> Project options... -> Compiler Options -> Config and Target -> uncheck Win32 gui application (-WG)
In Linux the console invokes itself.
Then the write and writeln commands can be used to print debug messages.