14
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

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

Embed Size (px)

DESCRIPTION

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)

Citation preview

Page 1: 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

Variables ContinuedIn 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 typesDifferent tasks you may perform with

different data types

Page 2: 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

Additional Data TypesThe following code is taken from the Widget

Swap program (btnSave of aswap.aspx). Notice some new data types.

Page 3: 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

The Date Data TypeWhat 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)

Page 4: 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

The Decimal, Integer & Byte Data TypesAll numeric data types. They differ in the kinds of

number they store Byte and Integer may only store whole numbersByte cannot store negative numbers and is limited to a

range of 0 to 255Integer has a range from -2,147,483,648 to

2,147,483,647 but like byte cannot store decimalsIf 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

Page 5: 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

Rounding Decimal NumbersSince 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.

Page 6: 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

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

Page 7: 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

The String Data Type & ConcatenationConcatenation involves the joining together

of smaller strings to form larger strings.

Page 8: 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

The Boolean Data TypeThe 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")

Page 9: 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

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?

Page 10: 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

Two ReasonsControls 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.

Page 11: 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

Unexpected ResultsImagine 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...

Page 12: 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

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.

Page 13: 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

Questions1. What is wrong with the following variable

declarations? Dim My Name As StringDim MyName As IntegerDim Age As DecimalDim Price As ByteDim Password As BooleanDim OK As Integer 

Page 14: 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

Questons2. 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 outputsAssign values to the input variablesWrite the code that calculates the answerDisplay the result on the web form