Monday, February 4, 2008

Chapter 3.3: Numbers

We went over numbers in the second to last class. We also learned about listboxes. A listbox is an object in which we can add things (like a list) one line at a time. There are two actions that we learned with listboxes:

listbox1.Items.Add( ... )
listbox1.Items.Clear()

Note: With the .Add() feature, the item that is to be added to the listbox is in the parentheses. Therefore we can add numbers to the listbox by placing them in the parentheses of .Add().

.Clear() simply erases whatever is in the listbox.

Example:

lstResults.Clear()
lstResults.Items.Add(8)
lstResults.Items.Add(4+3)
lstResults.Items.Add(3*9)


First the listbox is cleared (erased) with the Clear() statement. Then we will see the following:

8
7
27

Notice that the mathematic expressions are evaluated before the answer is printed.

Built-in Functions

We learned about some mathematical functions that we can use to make calculations easier:

  • Math.Sqrt()
  • Int()
  • Math.Round()


Math.Sqrt() returns the square root of the mathematical expression placed in the parentheses.

Int() returns the lower bound integer value of the mathematical expression in placed in the parentheses.

Math.Round() returns the upper bound real value of the mathematical expression in placed in the parentheses. We are able to specify the number of decimal places we would like to round the value to as well.

So the following code:

listbox1.Items.Add( Math.Sqrt( 16 ) )
listbox1.Items.Add( Math.Round( 9.2675 ) )
listbox1.Items.Add( Int( -9.2675 ) )

would yield the following results:

4
9.268
-10

Also remember that we can place any mathematical expression in the parentheses so we could even evaluate things as complicated as:

Math.Round( Math.Sqrt(19*20)/50+79, 3 )

Variables
A variable is just a name for a place in memory that we can use as temporary storage. We learned that we could use variables to store numerical values in order to make our arithmetic operations more flexible.

First we must declare the variable:

Dim varname As Type

varname can be any name we choose as long as it:
  1. begins with a letter
  2. uses legal characters such as letters, numbers and underscores
  3. is not used somewhere else in the program. For example the name of a form or listbox


The Type must also be given when the variable is declared. There are two types for numbers: Integer and Double. The type must be given when a variable is declared so that the compiler will know how much memory to allocate for the variable, and every type has a different amount of memory that it needs.

So here is an example declaration:

Dim num As Integer

You will notice that the intellisense menu will pop up as you are declaring variables.

No comments: