Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.
<<PreviousNext>>

The Assignment Statement

You are now ready to learn about numeric operations. Before doing so, however, take another look at the assignment statement. Consider the following statement:

HourPay = 12.50

The equal sign (=) in the statement instructs the computer to move data resulting from the operation(s) on the right side to the variable on its left side. Statements with this structure are recognized as assignment statements, and are the most common statements in nearly all programs. In addition, to assigning a constant to a variable, you can also assign the value of a variable to another variable:

HourPay = StandardHourPay

It is important to note that because the result on the right side will be moved to the left, any variables appearing on the right side must have been assigned with proper values before execution reaches the statement. In the previous statement, you assume StandardHourPay is another variable and has been declared and assigned some value before execution reaches this line.
You can also assign data entered in the VB control to a variable. For example, the following code will assign to the variable HourPay whatever number the user enters into the text box named txtHourPay.

HourPay = txtHourPay.Text

Tip
Keep in mind that the language has strict syntax rules that govern how each type of statements should be constructed. In the case of the assignment statement, there can be only one variable on the left side. The following statements will result in compile errors:
I, J = 1 I and J = 1  
Also, the result of the following statement may not be what you expected:
I = J = 1  
In some languages, this may mean I and J both are assigned a value 1. But in VB, this line means to compare whether J is equal to 1. If so, assign a value True to I; otherwise, assign False.

Notice that the left side of an assignment statement must be a variable or a control property whose value can be set at runtime. Notice also that the equal sign in the statement does not mean equal, but represents an instruction to move the result on the right side to the variable on the left. You can write a statement such as the following:

I = I + 1

This statement says to add 1 to the value of I and then move the result to I. The effect is that I is increased by 1. Note that VB 2008 has special syntax for operations that can produce the same effect. The syntax appears as follows:

Variable Operation = Operand

where Variable = the variable to appear on the left side of the assignment statement (the first operand)
Operation = the mathematical operation such as + (addition), - (subtraction), * (multiplication), or / (division)
Operand = the other (second) operand in the operation.
For example, the same effect of the preceding statement can be obtained with the following line:

I += 1

The following table shows the effects of a few examples using this special syntax:

Example Equivalent statement Effect
I += 2 I = I + 2 Increase the value of I by 2
I += I I = I + I Double the value of I
I -= 3 I = I - 3 Decrease the value of I by 3
I *= 3 I = I * 3 Triple the value of I
I /= 2 I = I / 2 Halve the value of I

Swapping Two Values

After being assigned a new value, the variable loses its previous content. It is important to remember that if you still need the original value of the variable, you will have to keep it in another variable before assigning a new value to it. For example, suppose you need to swap the values for two variables, named AdamsPay and JonesPay. The following code will not get the desired results:

AdamsPay = JonesPay JonesPay = AdamsPay  
This code fails because the first line assigns JonesPay (for example, 5,000) to AdamsPay. AdamsPay now has the value of 5,000, no matter what it had previously. This in effect results in both variables containing the same value, JonesPay. The second line assigns 5,000 to JonesPay, which was the original value of JonesPay anyway.

How do you solve this problem? As suggested, because you will need the original value of AdamsPay, you should find a way to keep its value before the variable is assigned with another value. You can do this by introducing a temporary variable to hold the original value of AdamsPay. After this is done, JonesPay can be assigned to AdamsPay, and the value in the temporary variable (Adams’ original value) can then be assigned to JonesPay. The diagram in Figure 4-2 shows how this algorithm works.

Figure 4-2
Swapping data between two variables

The following code accomplishes swapping the values of the two variables:

TempVar = AdamsPay AdamsPay = JonesPay JonesPay = TempVar

In the future, you may see many situations where, at the first glance, the problem appears pretty difficult to tackle. In such cases, your solution may lie in an introduction of an additional variable.

Try This
Draw a button onto a new form. Set its Name property to btnSwap and its Text property to Swap. Enter the following code. Run the program, click the button, and observe the result. You should be able to verify the swap.
Private Sub btnSwap_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnSwap.Click Dim TempVar As Single Dim AdamsPay As Single Dim JonesPay As Single AdamsPay = 5000 JonesPay = 7500 TempVar = AdamsPay AdamsPay = JonesPay JonesPay = TempVar MsgBox("After the swap, AdamsPay is " & AdamsPay & " JonesPay is " & JonesPay) End Sub
Last change: February 13 2016 18:47:34.
<<PreviousNext>>