Delphi 2 - Moving from VB to Delphi

By: Borland Staff

Abstract: change

Moving From VB to Delphi: Beginning to Code

    In this section, you'll begin to look at Delphi code-just enough to get you started. Of course, there is really no substitute for learning as much as you can about the Object Pascal language and it is beyond the scope of this paper to describe the entire language. However, there are enough similarities between Object Pascal and Visual Basic that a few notes should be enough to get you started. More language elements are covered later in this chapter.

    While the use of so-called "automatic variables" in BASIC is considered risky and unsafe, all Delphi variables must be explicitly declared. This is as if Option Explicit were used in VB.

    To create local variables in a procedure, you need to create a var clause in your procedure in which to declare them. Declaration of variables takes the form of


    where Name is the name of the variable and Type is the variable type. The colon takes the place of the As keyword in VB. The result in a procedure might look something like this:

    procedure TfrmMain.Button2Click(Sender: TObject);






    As you can see, it is possible to create a number of variables of the same type by simply separating their names with commas.

    With the exception of currency, all of the Visual Basic data types are available in Delphi as this table shows:

    VB Delphi
    Integer Integer
    Long LongInt
    Single Single
    Double Double
    String String, huge string
    Variant Variant

    Variables declared in the var section of a procedure are local to that procedure. There are, of course, several data types available in Delphi, not found in VB such a boolean, char and byte.

    Code Blocks
    One thing that you will notice about Object Pascal code right away is that all code blocks are surrounded by begin and end. That is why the event handler procedures that are generated by Delphi all have a begin..end section. This is the main executable code of your procedure. If there is a var section, it must come before the begin.

    In addition to the begin and end which surround the code of a procedure, there might be several code blocks within the procedure which require their own begin and end pairs. A good example of this is an if statement. Suppose you create a form with several buttons and a checkbox that determines the visibility of those buttons. The OnClick event of the checkbox might look something like this:

    procedure TForm1.chkShowClick(Sender: TObject);


    if chkShow.State = cbChecked then


    Button1.Visible := True;

    Button2.Visible := True;

    Button3.Visible := True;




    Button1.Visible := False;

    Button2.Visible := False;

    Button3.Visible := False;



    Don't worry too much about the structure of the if statement at this point but notice that there are two code blocks, each of which is surrounded by a begin and end pair.

    You will also notice that Delphi uses the semicolon to find the end of a statement, rather than the actual end of a line. Therefore, you may break up a line in any way you find most readable. For example, coming from Visual Basic, you might be most comfortable looking at an if statement like this:

    if chkShow.State = cbChecked then begin

    Button1.Visible := True;

    Button2.Visible := True;

    Button3.Visible := True;


    As you might imagine, a code block is defined as more than one line of code together. Therefore, a single line of code in an if statement doesn't require a begin..end block. In other words, the following code is perfectly valid, because only one line of code needs to be executed:

    if chkShow.State = cbChecked then Button1.Visible := True;

    Remembering to encase code blocks in begin..end pairs will be one of the most challenging "mental blocks" when moving from VB to Delphi.

    As you will have noticed, the assignment operator in Delphi is the := sign. This is distinguished from the comparison operator which is simply the = sign. This is like the distinction between = and == in C. Accordingly, the code to toggle the buttons above might look something like this instead:

    procedure TForm1.chkShowClick(Sender: TObject);


    Button1.Visible := chkShow.State = cbChecked;

    Button2.Visible := chkShow.State = cbChecked;

    Button3.Visible := chkShow.State = cbChecked;


    where chkShow.State = cbChecked is a Boolean expression, the result of which is assigned to the Boolean property Visible.

    Note In Delphi, you enclose string literals in single quotes, not double quotes as in VB. Therefore, to make an assignment to the caption of a form, you would do something like this:

    Form1.Caption := 'Hello World!';

    Commenting your code
    It is appropriate to mention the topic of comments early in the discussion of any development environment. In Delphi, you create a comment by surrounding text in curly braces. This is an example of a comment:

    {This is a comment}

    {This is an example of

    a multi-line comment}

    The equivalent of the ' comment for a single line in VB is // in Delphi as it is in C.

    //Here is a comment

    //This line of code does...

Server Response from: ETNASC03