VB Tutorial 6 Operators/Assignments
 

In our last tutorial we looked at Visual Basic looping and advanced conditional statements. In this tutorial we complete our overview of Visual Basic statements by examining operators and assignments. One cannot talk about assignment statements without also mentioning operators. There are two types of assignment statement - simple replacement and computational assignments. Here are examples.

simple replacement: IValue = 322 or Xdbl = val(i)
computational assignments: FV = (1+rate)^ nyears * PV or FinalString = Prefix & " starts the string " & Suffix


It is the computational assignments that are of the most interest to Visual Basic Programmers. In simple replacements as shown in the examples, Ivalue takes the value 322 and Xdbl's current value is replaced by the value of the array element val(i). However, in computational assignments a simple to quite complicated calculation is first performed and the resulting value is assigned to the variable.

Simple Assignments and Data Conversion

Simple replacement assignments are most often used to initialize variables. A research study showed that more programming errors are due to failure to initialize variables properly than any other cause. Part of the difficulty is that when two variables in an assignment are not of the same type automatic conversions are invoked that can change the value assigned in unexpected ways. In general, when a smaller variable type is assigned to a larger variable type (Integer to Double or Byte to String) no information is lost. But that is not always the case, as when a Date variable is assigned to just about any other variable type. Therefore Table 1 which lists common data conversions, is worth knowing well:

Table 1
Common Data Conversions in Visual Basic

Conversion Functions Example
Integer ANSI to String Chr Chr(66) ' returns "B"
Number to String Format, Str Format(99e2, "###,###") ' returns "9,900"
Date to String Format Format(Now, "mmmm yyyy") ' returns "August 1999"
String to Date DateValue, CDate CDate("09/99") ' returns 09/1/99
String to Number Val Val("-1.098e3") ' returns -1098
The most commonly required conversions are to and from Strings. Programmers are constantly converting out for display as strings on forms. likewise data input from forms must be converted strings to the proper data type as in our calculator program. So paying attention to data conversions on replacement assignments can eliminate a lot of time consuming bugs.

Computational Assignments

Computational assignments most often involve String or numeric operations but also can involve date manipulations, object references or other operations. We shall use the VB's Immediate Window to illustrate some of the rules of numeric and String manipulations.

Table 2 lists the major operators in Visual Basic - numeric or arithmetic, comparison operators (which we have already used in conditional statements) and logical operators(also used mainly in conditional statements). This also happens to be the order of precedence of the operators.

Why is precedence important ? Enter Print 990 + 3 + 7 / 3 in VB's Immediate Window. The answer is 995.333333333333 not a number close to 330 which some might have expected. Now try to guess what the value of Print -21 - 2 ^ 3 * - 3 will be.

Table 2
Visual Basic's Operators and their Precedence

Arithmetic

Comparison

Logical

^ exponentiation = equals Not - not
- negation <> not equals And - and
*, / multiplication and division < less than Or - or
\ - integer division > greater than Xor - exclusive or
mod modulus arithmetic <= less than or equal to Eqv - equivalence
+, - addition and subtraction >= greater than or equal to Imp - implication
& or + String concatenation Like a pattern matching operator
Is an object type comparer
 
If you guessed 3 then you know the rules of operator precedence. The computation is equivalent to (-21)-((2 ^3) * (-3)) = (-21)-(8 * (-3)) = (-21) - (-24)= -21 + 24 = 3. Note because ^-exponentiation is the highest precedence operator you do the operation 2^3 before any other. likewise because two operators come together, "* -", VB assumes the second operator is negation. In general arithmetic operations proceed comparison and then comparison come before logical.

Try to guess what these operations will do in the Immediate Window:

i= 999
Print I > 0 or i mod 3 > 99 and 3 > i ^ -5
Print Not I > 0 imp False

Figure 1 -VB's Immediate Window

I can't stress enough the utility of VB's Immediate Window for checking out your calculations and computational assignment statements. It really is worthwhile to verify logical comparisons you are not sure of in the Immediate Window. Also it easy to cut and paste to and from the Immediate Window. Finally you can assign values to all the variables used in an expression then test the expression. If you don't get the results expected just edit the expression right in the Immediate Window, press return and get new results.


Finally, the Like comparison operator is very useful when looking up filenames that match a pattern. The test fname like "b?t*.bak" matches "b1t1.bak" to "bztzzzzz.bak". It can be most useful.
Summary

In this tutorial we have completed our overview of Visual Basic syntax with a review of operators and assignment statements. VB operators are, with the possible exception of logical operators, fairly simple and easy to understand. Yet sophisticated commercial applications like SHL's Transform and Primavera's SureTrack have been built in Visual Basic. Indeed, with Microsoft Office 2000 and scores of other programs exposing their inner workings through VBA-Visual Basic for Applications and compatible components, expect to see more VB/VBA programs working directly with such programs as Microsoft Word or Excel and Visio 2000 and Corel Presentations or Draw, etc. Our next tutorial which returns to visual programming explores these prebuilt VB components.

Jacques Surveyer is a consultant and writer - he can be reached at theOpensourcery.com

Resources:

The book Beginning VB6/Wrox covers statement on pages 113-173; VB6 Black Book/Coriolis does the same on pages 36-96. Also check VB resources.

 
Top of Page  Tutorials Home