Essential Elements of a Program

Visual Basic

A program is simply a series of instructions that tell the computer what to do. Although programs can be complex, each individual instruction is generally simple. The computer starts at the beginning and works through, line by line, until it gets to the end. Here are some of the essential elements in Visual Basic:

Don't-Forget-New

The Visual Basic IDE is a safe environment in which to experiment and learn from your mistakes.

Statements
A statement is an instruction that performs an action. For example, the statement Lbl.BackColor = Color.Blue sets the background color of Lbl to Blue.

Functions
A function is a statement that returns a value. For example, the function InputBox() returns the value of its dialog text field.

Variables
A variable is a word defined in the program that stores a value. For example, the statement msg = “Hello World!” stores a string of characters in a variable called msg.

Operators
An operator is an arithmetical symbol. For example, the * asterisk character is the multiplication operator and the / forward slash character is the division operator.

Objects
An object is a program “building block” entity. It can be visible, like a Button control, or invisible like a Timer control.

Properties
A property is a characteristic of an object. For example, the property Btn.Text is the Text property of the Btn object.

Methods
A method is an action that an object can perform. For example, the method Btn.Click() is the Click method of the Btn object.

Comments
A comment is an explanatory line in the program code starting with an apostrophe ’ character. It’s not actually read by the compiler but exists to explain the purpose of the code. For example, ‘ Clear the list. might explain a Clear statement.

 

The illustration below shows the Code Editor view of Visual Basic programming code, for the Click event-handler of a Button control – line numbering is turned on to aid analysis of the code.

 

Line-by-line analysis

  • Lines 1 and 16 – start and end of the entire Form code
  • Lines 3 and 14 – start and end of the Button event-handler
  • Lines 5, 8, and 11 – explanatory comments
  • Line 6 – creates a variable called msg to store String data
  • Line 9 – places text value into the msg variable
  • Line 12 – calls the MsgBox() function to show the msg value

Syntax highlighting

  • Keywords – Visual Basic core language words appear in blue
  • Strings – text values, within double-quotes, appear in red
  • Comments – explanatory lines appear in green
  • Code – everything else appears in black

 

Hot-Tip-New_cropped
To turn on line numbering, click on Tools, Options, then expand Text Editor, Basic. Choose General then check the Line Numbers option.

 

 

Beware-New

 

The syntax colors shown here are the default colors. Custom colors can be chosen in the Tools, Options dialog, by expanding Environment, Fonts and Colors.

 

 

 

 

Visual Basic in easy steps, 4th Edition 9781840787016

Want to know more?

For the complete Visual Basic guide covering Visual Studio Community 2015, all in the trusted In Easy Steps style, click here. In full-colour and straightforward, jargon-free language, Visual Basic in easy steps, 4th edition shows you how to quickly create Windows applications using the latest free programming environment. This book gives you code examples, screenshots, and step-by-step instructions that illustrate each aspect of Visual Basic.

See also: Generating a random number in Visual Basic