Two of the most important things you control with a variable declaration are its data type and its visibility. Visibility combines scope (the piece of code that contains the variable such as a
For
loop, subroutine, or module), accessibility (the code that is allowed to access the variable determined by keywords such as
Private
,
Public
, and
Friend
), and lifetime (when the variable has been created and not yet
To avoid confusion, always explicitly declare the data type and use the most limited scope possible for the variable’s purpose. Turn Option Explicit and Option Strict on to allow the IDE to help you spot potential scope and type errors before they become a problem.
Parameters, property procedures, and constants have similar data type and scope issues. Once you become comfortable with variable declarations, these should give you little trouble.
One of the most important steps you can take to make your code easier to debug and maintain is to make your code consistent. A good naming convention can help. Review the guidelines used by Microsoft Consulting Service, and adopt the pieces that make the most sense to you.
When you know how to declare
An
operator
is a basic code element that
A = B + C
The Visual Basic operators fall into five categories: arithmetic, concatenation, comparison, logical, and bitwise. This chapter first explains these categories and the operators they contain, and then discusses other operator issues such as precedence, assignment operators, and operator overloading. Also included are discussions of some specialized issues that arise when you work with strings and dates.
The following table lists the arithmetic operators provided by Visual Basic. Most programmers should be very familiar with most of them. The four operators that may need a little extra explanation are \ , Mod , << , and >> . The last two rows in the table manipulate bit values.
|
Operator |
Purpose |
Example |
Result |
|---|---|---|---|
|
^ |
Exponentiation |
2 ^ 3 |
(2 to the power 3) = 2 * 2 * 2 = 8 |
|
- |
Negation |
-2 |
-2 |
|
* |
Multiplication |
2 * 3 |
6 |
|
/ |
Division |
3 / 2 |
1.5 |
|
\ |
Integer division |
17 \ 5 |
3 |
|
Mod |
Modulus |
17 Mod 5 |
2 |
|
+ |
Addition |
2 + 3 |
5 |
|
- |
Subtraction |
3 - 2 |
1 |
|
<< |
Bit left shift |
10110111 << 1 |
01101110 |
|
>> |
Bit right shift |
10110111 >> 1 |
01011011 |
The \ operator performs integer division. It returns the result of dividing its first operand by the second, dropping any remainder. It’s important to understand that the result is truncated toward zero, not rounded.
The Mod operator returns the remainder after dividing its first operand by its second. For example, 17 Mod 5 = 2 because 17 = 3 * 5 + 2.
The << operator shifts the bits of an integer value to the left, padding the empty bits on the right with zeros. For example, the byte value with bits 10110111 shifted 1 bit to the left gives 01101110. Shifting 10110111 2 bits to the left gives 11011100.
The >> operator shifts the bits of a value to the right, padding the empty bits on the left with zeros. For example, the byte value with bits 10110111 shifted 1 bit to the right gives 01011011. Shifting 10110111 2 bits to the right gives 00101101.
Unfortunately, Visual Basic doesn’t work easily with bit values, so you cannot use a binary value such as 10110111 in your code. Instead, you must write this value as the hexadecimal value &HB7 or the decimal value 183 . The last two entries in the table show the values in binary, so it is easier to understand how the shifts work.
| Tip |
The Calculator application that comes with Windows lets you easily convert between binary, octal, hexadecimal, and decimal. To start the Calculator, open the Start menu and Run. Type
calc
and click OK. Open the View menu and select Scientific. Now you can click the Bin, Oct, Dec, or Hex radio
|