Transcript PowerPoint
Variables Continued
In the last session we saw how variables are objects
that allow us to store values in the RAM of the
computer
In this session we shall take a look at:
Some other variable data types
Different tasks you may perform with different
data types
Additional Data Types
The following code is taken from the Widget Swap program
(btnSave of aswap.aspx). Notice some new data types.
The Date Data Type
What if I asked you to add 200 days to today’s date?
In VB.NET using the date data type it is easy...
Dim MyDate As Date
Dim NewDate As Date
MyDate = "23/4/2010"
NewDate = MyDate.AddDays(200)
The Decimal, Integer & Byte
Data Types
All numeric data types. They differ in the kinds of number
they store
Byte and Integer may only store whole numbers
Byte cannot store negative numbers and is limited to a range
of 0 to 255
Integer has a range from -2,147,483,648 to 2,147,483,647 but
like byte cannot store decimals
If we want to store larger numbers than those above or
want to store decimal values e.g. currency we need to use
the decimal data type.
The decimal data type has a range of +/-
79,228,162,514,264,337,593,543,950,335
Rounding Decimal Numbers
Since the decimal data type tends to go a bit over the top
with the number of decimal places it is worth knowing how
to round decimal numbers. The Math.Round method will
do this.
Dim Price As Decimal
Price = 25.657
txtPrice.Text = Math.Round(Price, 2)
The above code would round the price to 2 decimal places.
Mathematical Processing
Addition.
Addition operator.
Dim MyVariable As Integer
MyVariable = 5 + 2
Subtraction.
Subtraction operator
Dim MyVariable As Integer
MyVariable = 5 – 2
Multiplication.
The multiplication operator
Dim MyVariable As Integer
MyVariable = 15 * 2
Division.
The division operator
Dim MyVariable As Integer
MyVariable = 30 / 2
+
-
*
/
The String Data Type &
Concatenation
Concatenation involves the joining together of smaller
strings to form larger strings.
The Boolean Data Type
The Boolean data type is used to store the results of Boolean logic that
is based only on the values True and False. Boolean variables may only
store either True or False.
Later in the module we shall use the validation function called
IsNumeric which we will use to check if something is a number or not.
The following code gives you some idea of how it works...
Dim OK As Boolean
OK = IsNumeric("Fred")
Why don’t we just use Controls
and Abandon Variables?
Take a look at the following code…
Why don’t we just rewrite the code as follows?
Two Reasons
Controls have a visual component they are more
demanding on system resources. A variable is a much
more “light weight” object. Any code that constantly
uses controls to perform processing will run slower
than the same code written using variables.
Controls are typeless and run the risk of producing
strange results.
Unexpected Results
Imagine we have the following program that adds two
numbers together.
What do you expect will happen when we press Add?
The click event for “Add” contains the following code...
The Result…
The problem is that instead of performing a numeric
operation the code performs a string operation. It
concatenates rather than adds. This is one example of
the unpredictable results that may happen if we use
controls and not variables.
Questions
1. What is wrong with the following variable
declarations?
Dim My Name As String
Dim MyName As Integer
Dim Age As Decimal
Dim Price As Byte
Dim Password As Boolean
Dim OK As Integer
Questons
2. Plan out a program that takes two numbers, multiplies
them and displays the result.
What inputs are required?
What outputs are required?
Declare the variables for the inputs and outputs
Assign values to the input variables
Write the code that calculates the answer
Display the result on the web form