Transcript Data

Data
Tonga Institute of Higher
Education
Variables

Programs need to remember values.

Ex: If you use a program to record sales, you will want to
remember data:

A loaf of bread was sold to Sione Latu on 14/02/04 for T$1.00.




Customer Name: Sione Latu
Date Sold: 14/02/04
Item Sold: Loaf of Bread
Item Cost: T$1.00

Variables – Places in the computer’s memory where we
can store information.

Values and Objects are stored in variables.




Numbers
Characters
Strings
Dates
Numerical Data Types
•Primitives - The basic types supported by the programming language.
Type
.NET Class or Structure
Size
Value Range
Byte
System.Byte (Structure)
1 Byte
0 to 255
Short
System.Int16 (Structure)
2 Bytes
-32768 to 32768
Integer
System.Int32 (Structure)
4 Bytes
-2147483648 to 2147483648
Long
System.Int64 (Structure)
8 Bytes
-9223372036854775808 to
9223372036854775808
Single
System.Single (Structure)
4 Bytes
-3.402823E38 to -1.401298E-45
and 1.401298E-45 to 3.402823E38
Double
System.Double (Structure)
8 Bytes
-1.79768313486231E308 to
-4.94065645841247E-324 and
4.94065645841247E-324 to
1.79769313486232E308
Decimal
System.Decimal (Structure)
16 Bytes
-79228162514264339593543951335
to
.0000000000000000000000000001
and
.0000000000000000000000000001
to
79228162514264339593543951335
Non-Numerical Data Types
Type
.NET Class or
Structure
Value Range
Boolean
System.Boolean (Structure)
True or False
Date
System.DateTime (Structure)
12:00:00 midnight, January 1, 0001 AD
to
11:59:59 P.M., December 31, 9999 A.D
Char
System.Char (Structure)
A character
String
System.String (Class)
A string can contain up to approximately 2
billion (2 ^ 31) Unicode characters
Primitive Data Type
Selection Practice

How to choose the right primitive variable type.







Byte, Short, Integer and Long store whole numbers.
Single, Double and Decimal store fractions.
If you pick something that is too big, you’re wasting memory space.
Wasting memory space slows things down.
If you pick something that is too small, then you’re going to crash.
If you need a character, use Char.
If you need a true/false value, use Boolean.
What is a good data type for:




Someone’s age?
A customer’s identification number for a video rental store in
Nuku’alofa?
A very large number with decimals?
The price of an item?
Using Variables

2 Steps to using variables
1. Declare
the variable
2. Initialize the variable
Declaring Variables – 1

Declare the variable – Tell the computer to reserve a space in memory
for the variable.

You need to tell the computer 2 things:
1.
2.
Name of the variable
Type of the variable (What kind of variable you have)

Types











Byte
Short
Integer
Long
Single
Double
Decimal
Boolean
Date
Char
String
Type
Name
Declaring Variables – 2

Use a name that is easy to remember.



Do not use x, y, z
Begin each separate word in a name with a capital letter
Examples


FirstName
CustomerID
Initializing Variables

Initialize the variable – Assign an initial value to a variable.



Char values must be enclosed in double quotes and have a
lowercase c next to it.
String values must be enclosed in double quotes.
Boolean value should be True or False
Initial Value
Declaring and Initializing Variables
in 1 line

You can declare and initialize a variable in 1
line.
Demonstration
Declaring and Initializing
Variables
Converting Variable Types

You can convert variable
types through the Cast
functions













CBool()
CDbl()
CObj()
CByte()
CDec()
CShort()
CChar()
CInt()
CSng()
CDate()
CLng()
CStr()
If the Cast function isn’t able
to convert the value, you will
get an error.
Demonstration
Cast Functions
Converting Variable Values
Smaller to Larger

It is possible to automatically move a value from
a variable with a smaller type to a variable with a
larger type

Example
Maximum
Short Value
Maximum Long Value
Is 9223372036854775807
Converting Variable Values
Larger to Smaller

It is not possible to automatically move a value from a
variable with a larger type to a variable with a smaller type
Bigger than maximum
Short value

Using this:

Results in:

Try to not do this. If you must do this, then use the cast
functions.
Demonstration
Converting Variable Values
Option Strict, Option Explicit,
Option Compare - 1

Option Strict
 Use On to enforce the following rules:
 Conversions must done by the developer

Option Explicit
 Use On to enforce the following rules:
 All variable names must be declared.

Option Compare
 Use
as needed. Binary is good for most cases.
 Determines whether strings are compared as binary
strings or text.


Binary – “A” is not equal to “a”
Text - “A” is equal to “a”
Option Strict, Option Explicit,
Option Compare - 2



Each project can use default
settings. Set the default
settings in Tools -> Options ->
Projects -> VBDefaults
Each project can use it’s own
settings. Set the project
settings in Solution Explorer ->
Project -> Right Click ->
Properties -> Common
Properties -> Build
Each file can use it’s own
settings. Set the file settings
using this code at the
beginning of the code



Option Explicit On
Option Strict On
Option Compare Binary
Demonstration
Option Strict, Option Explicit,
Option Compare
Arithmetic Operators
Operator
Meaning
Example
^
Exponentiation
2^5
*
Multiplication
5*6
/
Division
21 / 3
+
Addition
11 + 22
-
Subtraction
22 – 11




Declare and Initialize x, y and
z
Get values from x and y
Adds x and y together
Assigns the sum of x and y to
z
Arithmetic Operator Details

Exponentiation
 Use

carrot (^)
Multiplication
 User

asterisk (*) instead of x
Division
 Make
sure that the variable
that holds a fraction allows
decimal points


Addition
Subtraction
Order of Operations

When you have a lot of operations, they are performed
in a certain order.




Operations in Parentheses ()
Exponentiation operations from left to right
Multiplication or Division operations from left to right
Addition or Subtraction operations from left to right

Please Excuse My Dear Aunt Sally.

Examples:



3 + 6 + 9 / 3 = 3 + 6 + 3 = 12
(3 + 6 + 9) / 3 = 18 / 3 = 6
(5 + 3) / 2 ^ 2 = 8 / 2 ^ 2 = 8 / 4 = 2
String Concatenation


String Concatenation – Adding strings together
Add strings using + or &
 Most
developers use & because it is better at
automatically converting values
String Concatenation - 2

There are 2 ways to add carriage returns to a string

Vbcrlf


Environment.NewLine


This is how this was done in previous versions of VB
This is the new way
Sometimes, string concatenation results in very long
lines of code. You can use _ to break up 1 long line into
multiple smaller lines.

Remember to follow code conventions


Indent all following lines
Put the & or + at the beginning of the next line
Demonstration
String Concatenation