compunet corporation1 programming with visual basic.net variables and data types week # 2 tariq ibn...

48
Compunet Corporation 1 Programming with Visual Basic .NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Upload: marshall-johnson

Post on 17-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Compunet Corporation 1

Programming with Visual Basic .NET

Variables and Data Types

Week # 2

Tariq Ibn Aziz

Page 2: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 2

Objectives

• Data Type Summary

• Variables and Assignments

• Strings Concatenation

• Constants and Char Data Types

• Integer and Real Number Data Types

• Date Time Data Type

• Default Values of Data Types

Page 3: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 3

Data Type Summary

• The table on next slides shows:– Visual Basic .NET data types, – their supporting common language runtime

types,– their nominal storage allocation,– and their value ranges.

Page 4: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 4

Data Type Summary

Visual Basic type

Common language

runtime type structure

Nominal storage

allocationValue range

Boolean System.Boolean 2 bytes True or False.

Byte System.Byte 1 byte 0 through 255 (unsigned).

Char System.Char 2 bytes 0 through 65535 (unsigned).

Date System.DateTime 8 bytes 0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999.Page 138

Page 5: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 5

Data Type Summary

Visual Basic type

Common language

runtime type structure

Nominal storage

allocationValue range

Decimal System.Decimal 16 bytes 0 through +/-9,228,162,514,264,337,593,543,950,335 with no decimal point;

      0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest nonzero number is +/-0.0000000000000000000000000001 (+/-1E-28).

Page 6: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 6

Data Type Summary

Visual Basic type

Common language

runtime type structure

Nominal storage

allocationValue range

Double System.Double 8 bytes -1.79769313486231570E+308 through

(double-precision floating-point)

    -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.

Integer System.Int32 4 bytes -2,147,483,648 through 2,147,483,647.

Page 7: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 7

Data Type Summary

Visual Basic type

Common language

runtime type structure

Nominal storage

allocationValue range

Long System.Int64 8 bytes -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807.

(long integer)

     

Object System.Object (class)

4 bytes Any type can be stored in a variable of type Object.

Short System.Int16 2 bytes -32,768 through 32,767.

Page 8: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 8

Data Type Summary

Visual Basic type

Common language

runtime type structure

Nominal storage

allocationValue range

Single (single-precision floating-point)

System.Single 4 bytes -3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values.

String System.String (class)

Depends on implementing platform

0 to approximately 2 billion Unicode characters.

Page 9: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 9

Data Types ???

• A reference type contains a pointer to another memory location that holds the data.

– Strings, classes, arrays, collections, and objects

• A data type is a value type if it holds the data within its own memory allocation.

– Referred to as primitive types or structures

– Stores the actual data in the stack

– Boolean and Char and Date

– All numeric data types

Page 10: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 10

Using Variables to Store Information

• “A variable is a temporary storage location for data in your program.” (see p. 127)

• A variable holds a value. There are many different kinds of values: numbers, letters, names, properties, etc.

• A variable has a name.• A variable has an address (location in memory).• A variable must be declared before it can be used.

Page 11: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 11

• It must Begin with a letter• It may have a letter [A-Z], [a-z],[0-9] and '_' length in

unlimited

• Do not use a digit in start

• No VB.NET reserved word can be used as variable name

• Do not use a period or space

• Avoid special characters except underscore

• First letter of each word is usually capitalized• VB.NET commands and variable are NOT case sensitive

• It must be unique within its scope

Variables Rules

Page 12: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 12

Selecting a Name for a Variable (continued)

Figure 3-4: Rules for variable names along with examples of valid and invalid names

Page 13: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 13

Identifiers

• An identifier is a name for an object (variable, function, etc.) in a program.

• Note: the following rules are for the C language:• Identifier must be made of letters, digits, and the

underscore ‘_’, but cannot start with a digit. They can be any length. Cannot be the same as a reserved word (such as if, end, etc.).

• Example– a, player1, Player1, STACK_SIZE are legal.– 1player, ten%, stack size, double are illegal.

Page 14: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 14

Variable Declarations• A variable must be declared before it can be used. Below is the syntax

for a variable declaration in VB:• Dim variableName As variableType

• “Dim” (short for dimension) and “As” are keywords and must appear as-is.

• variableName is the name of the new variable and is defined by the programmer.

• variableType must be one of the valid variable types.

• For example:

• Dim LastName As String

Page 15: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 15

Variable Declarations

• Purpose:• A variable declaration reserves space for the variable in

memory for use, as the program runs.

• Where:• The declaration for a particular variable must happen

before the first use of the variable in the program. • By convention, VB programmers declare all variables

together first inside a procedure, before the first executable statement.

Page 16: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 16

Variable Declaration (continued) First Three Characters Identify the Data Type

Data Type Prefix Sample Variable Name

Boolean Bln BlnMember

Byte Byt BytZero

Char Chr ChrLetter

Date Dat DatBirthdate

Double Dbl DblWeight

Decimal Dec DecProductPrice

Integer Int IntNumberProducts

Long Lng LngSalary

Single Sng SngAverage

Short Sho ShoYears

String Str StrLastName

Page 17: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 17

' compiler generates error, need to declare first

Intnum = 100

' max value of int is 2147483647 i.e 231-1

Dim IntNum As Integer = 100 // OK

' min value of int is -2147483648 i.e -231

Dim IntNum As Integer //OK

IntNum = 100 //OK

Variables and Assignments

Page 18: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 18

Module Module1Sub Main()

Dim SngPrice As Single

SngPrice=45.35

System.Console.Write ("This price is ")

System.Console.WriteLine (SngPrice)

End Sub

End Module

Output of this application is:The price is 45.35

Variables and Assignments

Page 19: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 19

Variables (Example)Imports System.Console Module Module1

Sub Main() Dim Chrc As Char = "X"

Dim Shos As Short= -32768

Dim Dbld As Double= 12.1234567890123

WriteLine ("char is " + Chrc) 'char is X

Write ("short is " )

WriteLine ( Shos ) 'short is -32768

Write ("double is ")

WriteLine (Dbld) 'double is 12.1234567890123

End Sub

End Module

Page 20: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 20

• Write a program that declares one integer variable called num. Give this variable the value 1000 and then, using one WriteLine() statement, display the value on the screen like this:

1000 is the value of num

Exercise(Variables and Assignments)

Page 21: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 21

StringsImports Microsoft.VisualBasicModule Module1

Sub Main() Dim Str1, StrFirst, StrLast, StrMid As String Str1 = "Mid Function Demo" StrFirst = Mid (Str1, 1, 3) ' "Mid". StrLast = Mid (Str1, 14, 4) ' "Demo". StrMid = Mid (Str1, 5) ' "Function Demo". System.Console.Write (Len(Str1)) '17 System.Console.Write (StrFirst) ' Mid System.Console.Write (StrLast) ' Demo System.Console.Write (StrMid) ' Function DemoEnd Sub

End Module

Page 22: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 22

StringsImports System.ConsoleModule Module1Sub Main() Dim StrFirst, StrLast As String StrFirst = "Tariq" StrLast = "Aziz"

WriteLine (StrFirst + StrLast) 'TariqAzizWriteLine (StrFirst & StrLast) 'TariqAzizWriteLine (StrFirst & " " & StrLast) 'Tariq Aziz

End SubEnd Module

Page 23: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 23

Assignment statements

• An assignment statement is an executable statement that results in storing a value in a variable. It is not an algebraic equation!

• Syntax:• variable = expression

• The assignment operator is the equal sign (=) • <EOL> is the statement terminator.• The lvalue is an expression that evaluates to a memory

location.• The rvalue can be a constant, variable, or complex

combination including operators.

Page 24: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 24

Assignment Statement Examples

Dim x As IntegerDim y As IntegerDim z As Integerx = 1y = 2z = x + yx = x + 1

– What is the value of x after the code above executes?– Note: Sequence, selection, and repetition– See also page 130

lvalue

rvalue

Page 25: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 25

Assignment Statement Examples

– Remember Visual Basic .NET is NOT case sensitive. However, Visual Studio .NET will place Dim, As, and String in upper & lower case characters

Dim StrLastName, StrFirstName As String

Dim StrStoreName As String = "Tara Store"

Page 26: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 26

String

• Several built-in methods manipulate strings– LCase and UCase - converts case to lower and upper

Imports Microsoft.VisualBasicPublic Module StringMethods Sub Main()

Dim StrName As String="TARIQ AZIZ"

System.Console.print(LCASE(StrName)) End SubEnd Module

Page 27: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 27

Constants• A variable that does not change

• Examples - tax rates, shipping fees, and values used in mathematical equations

• Declare a constant

– Const keyword - Name is usually all uppercase

– When you declare a constant, you need to assign the value to the constant

– Const TAXRATE As Integer = 8– Const Pi As Double = 3.14159– Const greeting As String = “Good morning!”

Page 28: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 28

Constants Exercise

• Write a small VB program that computes the area of a circle given its radius. Your program should include a constant declaration for the value of Pi.

Page 29: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 29

Concatenation • Process of joining one or more strings

• Concatenation operator (&)– Can also use (+) only with strings

• Join a literal string, or the result returned from an expression, or a variable that contains a string

Dim str As String

Str= "Tariq" + "Aziz"

Str = Str & "Lecturer CA121"

Page 30: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 30

Expressions

• Sequence of operands and operators that reduces to a single value.33 + 23 + 2 * 4

• What are the operands of the *? What about +?• A primary expression consists of only a single operand

which has no operator. It can be a name, a literal constant, or a parenthetical expression.

• A binary expression consists of one operator and two operands.

• Operators: Binary: +, -, *, /, \, Mod– Unary: +, -

Page 31: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 31

Option Explicit and Option Strict• Option Explicit On statement

– Prevents you from using undeclared variables

• Implicit type conversion

– Converts right-side value to datatype of left side

– Promotion: data expanded; e.g., Integer to Decimal

– Demotion: data truncated; e.g., Decimal to Integer

• Option Explicit On statement

– Suppresses implicit conversions

Page 32: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 32

Option Explicit and Option Strict (continued)

Figure 3-19: Rules and examples of implicit type conversions

Page 33: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 33

Option Explicit and Option Strict (continued)

Figure 3-20: Option statements entered in the General Declarations section

Page 34: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 34

+ Operator

Dim IntNumber As IntegerDim Strvar1 As StringDim Strvar2 As IntegerIntNumber = 2 + 2 ' Returns 4.IntNumber = 4257.04 + 98112 ' Returns 102369.

Option Strict On' Initialize mixed variables.Strvar1 = "34"Strvar2 = 6 IntNumber = Strvar1 + Strvar2' Generates a compile-time error, disallows' implicit conversion from "String" to "Double".

Page 35: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 35

+ Operator

Option Strict OffStrVar1 = "34"IntVar2 = 6IntNumber = StrVar1 + IntVar2

' Returns 40 (addition) after the string in var1' is converted to a numeric value. ' Use of Option Strict Off for these operations' is not recommended.

Page 36: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 36

Assigning Data to an Existing Variable (continued)

Figure 3-7: Literal type characters

Page 37: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 37

Char• Char variables are stored as unsigned 16-bit (2-byte) numbers • Store a single text value as a number between 0 and 65,535• Represents a character in categories such as digit, letter,

punctuation, and control characters

Dim ChrVar As Char' Cannot convert String to Char with Option Strict OnChrVar = "Z" ' Error.ChrVar = "Z"C ' Successfully assigns characterDim MyChar As CharMyChar = Chr(65) ' Returns "A".MyChar = Chr(97) ' Returns "a".MyChar = Chr(62) ' Returns ">".MyChar = Chr(37) ' Returns "%".

Page 38: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 38

Numeric• Integer number data types

– Byte - stores an integer between 0 and 255

– Short - 16-bit number from -32,768 to 32,767

– Integer - 32-bit whole number

– Long - 64-bit number

• Real number data types – Single - a single-precision floating point number

– Double - larger numbers than the single data type

– Decimal - up to 28 decimal places and often used to store currency data

Page 39: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 39

Byte and Short Data Type

Dim b As Byteb=20*12 ' 240

b=20*15 ' Error

Dim s As Shorts=2^15 -1 ' 32767

Page 40: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 40

Integer and Long Data Type

Dim i As Integeri=2^31 -1 ' 2147483647

i= 2147483647I ' 2147483647

i= 2147483647% ' 2147483647

i=-2^31 ' -2147483648

Dim l As Longl=9223372036854775807

l=9223372036854775807&

l=-2^63& ' -9223372036854775808

Page 41: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 41

Single Data Type

• Single-precision numbers store an approximation of a real number

• The Single data type can be converted to the Double or Decimal data type

Dim d1 As Single = 2 ^ 10.5 ' 1448.155Dim d2 As Single = -2 ^ 10.5! ' -1448.155Dim d3 As Single = -2 ^ 10.5F ' -1448.155

• Appending the literal type character [ F or ! ] to a literal forces it to the Single data type

Page 42: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 42

Double Data Type

• Double variables are stored as signed IEEE 64-bit (8-byte) double-precision floating-point numbers

Dim d1 As Double = 2 ^ 10.5 ' 1448.15468787005Dim d2 As Double = -2 ^ 10.5R ' -1448.15468787005Dim d3 As Double = -2 ^ 10.5# ' -1448.15468787005

• Appending the literal type character [ R or # ] to a literal forces it to the Single data type

Page 43: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 43

Decimal Data Type

' No overflow.Dim d As Decimal = 9223372036854775807' overflow.Dim d As Decimal = 9223372036854775808' No overflow.Dim d As Decimal = 9223372036854775808D' No overflow.Dim d As Decimal = 9223372036854775808@

• This is because without a literal type character [D or @ ] the literal is taken as Long

overflow

Page 44: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 44

DateTime

• Dates between 01/01/0001 and 12/31/9999

• Formats are mm/dd/yyyy and hh:mm:ss

• Enclosed within a pair of pound signs

Dim MyBirthday As DateTime MyBirthday = #3/22/2002#

Page 45: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 45

Boolean Data Type

• Two possible values: True or False

• In binary math: – 1 represents true

– 0 represents false

• When numeric data types are converted to Boolean values, 0 becomes False and all other values become True. When Boolean values are converted to numeric types, False becomes 0 and True becomes -1.In Visual Studio .NET

– True value is converted to -1

– False value is converted to 0

FalseTrue

Page 46: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 46

Default Values

• The default value of decimal is equivalent to the literal 0D.

• The default value of an integral type (Byte, Short, Integer and Long) is equivalent to the literal 0

• The default value of Boolean is False.

• The default value of the Date type is equivalent to the literal # 01/01/0001 12:00:00AM #.

• The default value of the Char type is ChrW(0).

• The default value of the String type is a null reference.

Page 47: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 47

User-defined Data Types (UDT)

• The programmer can invent his own type using a structure statement. A structure can include multiple component variables (analogous to fields in a database record).

Structure Student Dim Name As String Dim GPA As Double Dim id As LongEnd Structure. . .Dim s1 As Students1.Name = “Saleh Al-Ghamdi”

Page 48: Compunet Corporation1 Programming with Visual Basic.NET Variables and Data Types Week # 2 Tariq Ibn Aziz

Feb 23, 2007 Dammam Community College 48

User-defined Data Types Example

Module M1Structure Student

Dim Name As String Dim GPA As Double

Dim id As LongEnd Structure

Sub main()Dim s1 As Students1.Name = “Saleh Al-Ghamdi”s1.GPA=3.5s1.id=200410213system.console.writeline(s1.Name &","& s1.GPA &","& s1.ID)End Sub

End module