Learn the most popular programming language of the most popular Operating System: Windows

Ad

Coded Statements and Methods

In the preceding procedure, you wrote several coded instructions, which introduced two different programming statements and a method: the Dim statement, an assignment statement, and the Print method.  You also added a remark or comment to your code and used the Private keyword.

The Dim Statement

The Dim statement is used to declare variables and to allocate storage space.  The statement begins with the keyword Dim, as in:

Dim message As String

It declares the variable message as a string of characters. As a string, it can contain characters, including blank spaces and punctuation, as well as numbers.

When typing statements, observe that the Visual Basic editor changes keywords to blue. If your statement is correct, the word Dim and the words As String appear in blue. This tells you that the syntax used in writing the statement is correct. If you make a mistake, as in the following code in which the word String is misspelled, the blue markings will not appear:

Dim message As Sting

The Assignment Statement

With an assignment statement, an equation is used. The value expression on the right side is assigned to the variable on the left. The syntax for the assignment statement is as follows:

[Let] variable = valueexpression

The syntax states that the keyword Let is optional. The dollar sign ($) is also optional, you could have written the following:

message$ = "Welcome to Visual Basic"

Because the dollar sign ($) is optional, you could have written the following, for message has been declared as a variable of type String:

message = "Welcome to Visual Basic"

In general, use a dollar sign to help distinguish a string variable from another type.

A string expression must begin and end with quotation marks. If you write the following, omitting the quotation marks, you will be in error:

message = Welcome to Visual Basic

The Remark Statement

In Visual Basic, any statement that begins with a single quotation mark (') or the keyword Rem is a remark, as follows:

Rem The Print command is used to display a message

Remarks or comments are used to describe the purpose of a procedure as well as key features of a procedure. Use remark statements to document how your code works and to provide other type of important coding information. Also, be careful when writing the statement. Use the apostrophe (') which is the same as single quotation mark and shares the quotation mark (") key, and not the grave accent (`), which can be found on the tilde (~) key. In writing the procedures, we used the apostrophe rather than Rem. This enables us to place remarks to the right of an instruction, as in the following example:

Print 'This instruction displays a blank line

When writing remarks, use the color code returned by the editor to determine whether your statements are correct or incorrect. A correct remark statement will, by default, be shown in green.

The Private Keyword

All control procedures are preceded by the Private keyword by default. This keyword makes the procedure accessible only in the form module in which it is declared. The meaning and use of the Private keyword, as well as other keywords that can be used in procedure declarations, are discussed in detail under "Defining the Scope of Variables and Procedures," when you consider the topic of variable and module scope. For the time being all procedures you write will use the Private keyword.

The Print Keyword

The single method used in Exercise 3.2 was the Print method. A method differs from a statement, such as an assignment statement, by specifying the behavior of an object. The following statement features a period (.) to separate the object from the action to be taken:

Form1.Print "Message to be printed"

That, in turn, is followed by a space, so that the following instruction would be in error:

Form1.Print="Message to be printed"

The syntax used for the Print method itself is quite complex and deserves special attention. You consider that notation next. Once you understand this syntax, you will be able to understand the syntax designed for all methods.

Share:

Trending

Popular Posts

Search This Blog

Powered by Blogger.

Featured Post

Coded Statements and Methods

In the preceding procedure , you wrote several coded instructions, which introduced two different programming statements and a method: the D...

Recent Posts