chapter 4 notes - Kilby

Document Sample
chapter 4 notes - Kilby Powered By Docstoc
					                                        Chapter 4

If a user enters information into a textbox we can perform an operation on that value and
display it somewhere else. However, what if the user is going to enter a second value
into the same textbox and we want to perform an operation using the first and second
values? Since a textbox can only hold one value at a time we need some way of
remembering the previous value. To store values in a program we use variables. The
amount of variables we have is limited only by the amount of RAM our computer has.
Basically, making a variable, also known as declaring a variable, is just assigning a
meaningful name to a memory location in the computer’s RAM.

To declare a variable in VB we use the following syntax:

Dim variable_name As type

There are several different types we could use when declaring a variable. We could use
Single or Double, for numbers with a decimal point, Integer or Long, for numbers
without a decimal point, and String, for variables that must contain letters and other

Dim strName As String
Dim intTotal As Integer
Dim sngAverage As Single

To place a value into a variable we could use the equal sign followed by a value or the
text from a control, such as a textbox.

strName = “Joe Chan”
intTotal = Me.txtInput.text
sngAverage = Me.txtInput.text / 2
Dim intCount as Integer = 0

Remember that a variable can only store one value at a time so changing its value with
the equal sign causes the old value to be replaced with the new one. Furthermore, when
assigning input from a textbox to a number variable, there is a chance that the user may
have entered a non-number. To prevent the computer from generating an error we could
use the function Val. The Val function will convert the contents of a textbox into a
number, if possible, or zero if the textbox has non-digits in it.

Dim sngNum as Single = Val(Me.txtInput.text)
If we wish to store a value but the value will never change then we still use a memory
location but we can use that memory location as a constant rather than a variable. A
constant found in math is pi.

Const sngPi As Single = 3.14

The button click event handler for a program that gets a radius for a circle from the user
and then displays its area when a button is clicked could look like this:

Private Sub …
    Const sngPi As Single = 3.14
    Dim sngRadius as Single
    Dim sngArea as Single

   sngRadius = Val(Me.txtRad.text)
   sngArea = sngPi * sngRadius ^ 2
   Me.lblDisplay.text = sngArea
End Sub

The names for constants or variables are called identifiers and all identifiers must begin
with a letter and contain only letters, digits and the underscore character. You cannot
have an identifier with periods, spaces or other special characters.

Lastly, you may only use your variables or constants in certain parts of your program.
The notion that identifiers only can be used in certain places is referred to as the scope of
the identifier. Basically, if you declare something in an event handler it can only be used
in that event handler. If you wish several event handlers to use the same identifier then
declare it at the top of your program before any of your event handlers.

Shared By: