101
Visual Basic .NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

Embed Size (px)

Citation preview

Page 1: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

Visual Basic .NETComprehensive Concepts

and Techniques

Chapter 4

Working with Variables, Constants, Data Types,

and Expressions

Page 2: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

2Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Objectives

• Use the RadioButton and GroupBox controls

• Set a default button on a form

• Declare variables and constants

Page 3: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

3Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Objectives

• Use variables and constants within code

• Describe Visual Basic .NET data types

• Convert between data types

• Code a form Load event procedure

• Use the Option Strict statement

Page 4: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

4Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Objectives

• Use arithmetic expressions

• Describe the order of operator precedence in code

• Use the Pmt function

• Use the Format$ function

Page 5: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

5Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Page 6: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

6Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Starting the Project and Creating the User Interface• Start Visual Basic .NET and click the New

Project button on the Start Page

• Name it Automobile Loan Calculator

Page 7: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

7Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Setting Form Properties and Adding Controls

Page 8: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

8Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Setting Form Properties and Adding Controls• Add three Label controls, two

NumericUpDown controls, one TextBox control, and two Button controls

Page 9: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

9Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Adding a GroupBox Control to a Form• Add the GroupBox control by clicking the

GroupBox button in the Toolbox window and drawing the GroupBox1 control on Form1, as shown below

Page 10: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

10Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Adding RadioButton Controls to Create a Group of Controls• Drag the RadioButton button from the

Toolbox window to the top of the GroupBox1 control. Be sure to release the mouse button while the mouse pointer is positioned within the borders of the GroupBox1 control

• Repeat this step to create two more RadioButton controls

Page 11: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

11Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Changing Control Properties

• Change the properties of the Label, NumericUpDown, TextBox, and Button controls according to Table 4-4 on page 4.14 and 4.15 in the text

Page 12: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

12Chapter 4: Working with Variables, Constants, Data Types, and Expressions

GroupBox Control Properties

Page 13: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

13Chapter 4: Working with Variables, Constants, Data Types, and Expressions

RadioButton Control Properties

Page 14: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

14Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Setting the Default Button on a Form• Pressing the ENTER key is equivalent to

clicking the button

• Select the AcceptButton property in the properties window for Form1 to btnComputePayment

Page 15: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

15Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Constants and Variables

• A value is a number or string that programmers use in the code

• A variable represents a location in computer memory that can change values as the code executes

• A constant represents a location in computer memory that cannot be changed during execution

Page 16: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

16Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Data Types

Page 17: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

17Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Constants and Variables

• Use a value when it shows up only once in the code.

• Use constants to store values that do not change and may be used more than once.

• Use variables to store values that may change as the code executes.

Page 18: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

18Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Rules for Naming Constants and Variables

• The name must either :– begin with a letter, or– Begin with an under score followed by at least one valid

character.

• The name cannot contain punctuation or blank spaces.

• The name is not case sensitive (e.g. ScoRe and score are the same variables/constants).

• The name can be up to 16,383 characters in length.

Page 19: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

19Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Variables The general forms:

Dim name as datatype = initial value

Dim name1 as datatype = initial value, name2 as datatype = initial value, ..

Dim name1, name2,… as datatype

Dim name1 as datatype, name2 as datatype, name3 as datatype,…

Dim name1 = initial value, name2 = initial value, name3 = initial value,

Dim name1, name2, name3, …

Dim name1 as datatype = initial value, name2 as datatype, name3 as datatype = initial value,…

Page 20: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

20Chapter 4: Working with Variables, Constants, Data Types, and Expressions

e.g. date

Dim Birthday As Date

Birthday = #11/27/1963#

t1.text = Birthday

textbox control

Page 21: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

21Chapter 4: Working with Variables, Constants, Data Types, and Expressions

e.g. char

Dim cat As String

cat = “nasa"

Dim ch as char

ch = “a"

Page 22: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

22Chapter 4: Working with Variables, Constants, Data Types, and Expressions

e.g. short and boolean

Dim Birds As Short

Birds = 12500

Dim Flag as Boolean

Flag = True

Page 23: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

23Chapter 4: Working with Variables, Constants, Data Types, and Expressions

e.g. dim x : MsgBox(x)

x =20: MsgBox(x)

X = “A” : MsgBox(x)

X = true: MsgBox(x)

Page 24: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

24Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Dim x, x1 = 20

error

Page 25: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

25Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Constantsconst name as datatype = initial value

const name1 as datatype = initial value, name2 as datatype = initial value, ..

const name1 = initial value, name2 = initial value, name3 = initial value,

Page 26: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

26Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Constants

• Double-click the Form1 form in an area that does not contain a control. When the code window displays, click 189

• Enter the seven lines of code below, and press the ENTER key after entering the last line

Page 27: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

27Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring a constant

• The first Form:

Const name As type = value

• The second Form:Const name = value

key words

Page 28: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

28Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring a constant

• E.g1:

Const MaxVolume as Integer = 11

• E.g.3: Const capacity = 150.3

Page 29: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

29Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Choosing the data Types

• Try to use the data type that takes up the smallest amount of memory.

e.g. if you need to use a variable that takes a value between 0-150 then the best type is Byte. It is waste to use Long for instance.

Page 30: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

30Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Choosing the data Types

• Try to use integral data type that represent whole numbers because arithmetic operations are faster with the whole numbers.

• If a variable or constant will not contain a decimal, use nonintegral data type.

Page 31: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

31Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Using the option strict statement

• VB.net does not require you to use the same data type on both sides of the assignment.

• VB.net converts the variables or constants to the proper data type on both sides of the assignments.

• This conversion can cause problems and be unreadable for others.

Page 32: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

32Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Using the option strict statement

• If you type: Option strict on

– The option strict statement forces VB.net to accept the assignment statement only if it has the same type in both sides.

– The option strict statement forces you to declare a data type for all variables and constant.

– Must be the first line in the code entered in the code window.

Page 33: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

33Chapter 4: Working with Variables, Constants, Data Types, and Expressions

e.g.

Option strict on

Public Class Form1

Inherits System.Windows.Forms.Form

' =====================

Const x As Integer = 5

……

……

Page 34: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

34Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Using the option strict statement

• If you type: Option strict off

– The option strict statement does not force VB.net to accept the assignment statement only if it has the same type in both sides.

The default

Page 35: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

35Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict on

const name = value

dim var

dim var = value

dim var1, var2, var3,…..

Error since there are no types

Page 36: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

36Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring variables

• E.g. Option strict on

const capacity = 150.3 Dim score as integer = 0

Dim name as string

Dim address

Error: Option Strict On requires all variable declarations to have an 'As' clause.

Page 37: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

37Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring variables/constants

• The scope refers to the places inside the program from which you can access (use) a variable or a constant.

• The scope of a variable or a constant depends on the place of declaring this variable/constant.

• Declaring a variable/constant can either be:

– Local or – Global

Page 38: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

38Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring variables/constants

– Local: a variable or a constant is local if it defined within a procedure. It has scope within that procedure only.

– Global : a variable or a constant is global if it is declared in the general area of the code (any place outside the procedures and inside the program)

Page 39: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

39Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data Type

• Convert.toboolean(x) method converts the value of a variable/constant X to a true or false values.

E.g. Dim z As Boolean = True

z = Convert.ToBoolean(-1)

z = Convert.ToBoolean(0)

The result: z = true The result: z

= falseIf 0 then z is false

Any other value z is true

z = CBool(-1)

z = CBool(0)The same

Page 40: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

40Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data Type

• Convert.tostring(x) method converts the value of a variable/constant X to a string.

E.g. Dim z As string

Dim x As double = 3.245

z = Convert.Tostring(x)

The result Z = ”3.245“

z = CStr(x)The same

Page 41: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

41Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data Type

• Convert.toint16(x) 0r CShort(x) method converts the value of a variable/constant X to short.

• Convert.toint32(x) or CInt(x) method converts the value of a variable/constant X to integer.

• Convert.toint64(x) or CLng(x) method converts the value of a variable/constant X to long.

• E.g. option strict off ………

Dim z, y, k As Integer z = Convert.ToInt16(34.24) y = Convert.ToInt32(34.54) k = Convert.ToInt64(34.94)

The result z = 34

The result y = 35The result k = 35

If the resulting value is greater than the maximum value that the short can save then the program will hang

Page 42: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

42Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data Type

• E.g. option strict on Dim z, y, k As Integer

z = Convert.ToInt16(34.24)

y = Convert.ToInt32(34.54)

k = Convert.ToInt64(34.94)The result z = 34

The result y = 35Error :cannot convert from long to int

Page 43: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

43Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict OnPublic Class Form1 Private Sub Button1_Click(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles Button1.Click

T1.Text = 3 + 2 MsgBox(T1.Text) End SubEnd Class

Error 1 Option Strict On disallows implicit conversions from 'Integer' to 'String'.

Page 44: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

44Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict OffPublic Class Form1 Private Sub Button1_Click(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles Button1.Click

T1.Text = 3 + 2 MsgBox(T1.Text) End SubEnd Class

Page 45: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

45Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict OnPublic Class Form1 Private Sub Button1_Click(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim x As Integer x = 5 * 3 + 4 + 2 MsgBox(x) End SubEnd Class

Page 46: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

46Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict OnPublic Class Form1 Private Sub Button1_Click(ByVal sender As

System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim x As Integer x = 5 * 3 + 4 + 2.0 MsgBox(x) End SubEnd Class

Error 1 Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

Page 47: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

47Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data TypeOption Strict OffPublic Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim z, y, k As Integer z = Convert.ToInt16(34.24) y = Convert.ToInt32(34.54) k = Convert.ToInt64(4123456789012)

End SubEnd Class

Error: Arithmetic operation resulted in an overflow.

Page 48: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

48Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data TypePublic Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim z As Integer

z = Convert.ToInt16(4123456789012)

End Sub

End Class

Error: value was either too large or too small for an Int16.

Page 49: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

49Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data Type

• Convert.todouble(x) or CDbl(x) method converts the value of a variable/constant X to a double.

Dim z As double

z = Convert.Todouble(34)

The result Z = 34.0

Page 50: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

50Chapter 4: Working with Variables, Constants, Data Types, and Expressions

option strict off

Dim x As Integer = 300

Dim y As Byte

y = x

Error: overflow

Page 51: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

51Chapter 4: Working with Variables, Constants, Data Types, and Expressions

option strict off

Dim x As Integer = 200

Dim y As Byte

y = x

No error

Page 52: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

52Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict off

Dim x As Integer

x = "23“

No error

Page 53: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

53Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict off

Dim x As Integer

x = "23st“

error

Page 54: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

54Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict off

Dim x As Intege

x = “A“

error

Page 55: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

55Chapter 4: Working with Variables, Constants, Data Types, and Expressions

option strict on

Dim x As Integer = 300

Dim y As Byte

y = x

Error 1 Option Strict On disallows implicit conversions from 'Integer' to 'Byte'.

Page 56: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

56Chapter 4: Working with Variables, Constants, Data Types, and Expressions

option strict on

Dim x As Integer = 200

Dim y As Byte

y = x

Error 1 Option Strict On disallows implicit conversions from 'Integer' to 'Byte'.

Page 57: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

57Chapter 4: Working with Variables, Constants, Data Types, and Expressions

option strict on

Dim x As Integer = 200

Dim y As Byte

x = y

No error

Page 58: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

58Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict on

Dim x As Integer

x = "23“

Error 1 Option Strict On disallows implicit conversions from 'String' to 'Integer'.

Page 59: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

59Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict on

Dim x As Integer

x = "23st“

Error 1 Option Strict On disallows implicit conversions from 'String' to 'Integer'.

Page 60: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

60Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict on

Dim x As Integer

x = “A“

Error Option Strict On disallows implicit conversions from 'String' to 'Integer'.

Page 61: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

61Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict off

Dim x As Integer

x = 3 + "6" + 2

MsgBox(x)

The result is x = 11

Page 62: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

62Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict on

Dim x As Integer

x = 3 + "6" + 2

MsgBox(x)

errorOption Strict On disallows implicit conversions from 'String' to 'Double'.

Page 63: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

63Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict OffDim x As Integerx = 3 + "6kk" + 2MsgBox(x)

errorConversion from string "6kk" to type 'Double'

is not valid.

Page 64: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

64Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict Off

Dim x As String

x = 3 + "6" + 2

MsgBox(x)

The result is x = 11

Page 65: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

65Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict OnPublic Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim x As String x = 3 + "6" + 2 MsgBox(x)

End SubEnd Class

Error 1 Option Strict On disallows implicit conversions from 'String' to 'Double'.

Page 66: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

66Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict off

Dim x As Integer

x = 3.5

MsgBox(x)

The result is x = 4

Page 67: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

67Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Exercise: try the following

Option Strict Off

Option Strict On

Dim x As Integer

x = 3.5

MsgBox(x)

Page 68: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

68Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option strict on

Dim x As Integer

x = 3.5

MsgBox(x)

Error 1 Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

Page 69: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

69Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict On

Dim x As Integer

x = 4 Mod 3

MsgBox(x)

The result is x =1

Page 70: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

70Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Option Strict On

Dim x As Integer

x = 2 + 5 * 2.0

MsgBox(x)

Error 1 Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

Page 71: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

71Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Numeric Expressions and Operator Precedence

• An expression can perform a calculation, manipulate characters , call a function or test data.

• A numeric expression is any expression that can be evaluated as a number.

Page 72: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

72Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Numeric Expressions and Operator Precedence

• A numeric expression can include values, variables, constants, control properties and arithmetic operators (^, *, /,\,mod,+,-).

• A numeric expression cannot contain string variables, string constants or objects.

Page 73: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

73Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Arithmetic operator

• ^ (e.g. the result of 2^3 is 8)• * (e.g. the result of 2*3 is 6)• / used to divide two numbers and

return a decimal result. (e.g. the result of 3/2 is 1.5).• \ used to divide two numbers and

return an integer result. (e.g. the result of 3/2 is 1):

– The backslash operator (\) first round the dividend and the divisor to integers, then

– it truncates any decimal portion of the quotient.

– E.g. 5\2 is equal to 2, 8.9 \ 2.4 is equal to 4

Page 74: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

74Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Arithmetic operator

• Mod (e.g. the result of 5 mod 2 is 1).• + (e.g. the result of 3 + 2 is 5).• - (e.g. the result of 3 - 2 is 1).

Page 75: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

75Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Numeric Expressions. E.g.

• The following expressions are invalid: A = 2b

C = 6 + “debit” /c

D = “25” /b +”x” -19

It should be 2 * b

Page 76: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

76Chapter 4: Working with Variables, Constants, Data Types, and Expressions

The precedence in the numeric expression

• A numeric expression is evaluated from left to right according to the following precedence as follows:– Parenthesis: ()– Exponentiation: ^– Multiplication: *, division: /– integer division: \– Mod operator: mod– Addition: + and subtraction: -

Page 77: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

77Chapter 4: Working with Variables, Constants, Data Types, and Expressions

The precedence in the numeric expression

• E.g. 18/3 ^ 2 + 4 * 2

18/9 + 4 * 2

2 + 4 * 2

2 + 8

10

Page 78: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

78Chapter 4: Working with Variables, Constants, Data Types, and Expressions

The precedence in the numeric expression

E.g. 3 * 9 mod 2 ^2 + 5\4.8/2-3 3 * 9 mod 4 + 5\4.8/2-3

27 mod 4 + 5\4.8/2-3 27 mod 4 + 5\4.8/2-3

27 mod 4 + 5\2.4-327 mod 4 +2-3

3 + 2 -35 -3

2

Page 79: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

79Chapter 4: Working with Variables, Constants, Data Types, and Expressions

The precedence in the numeric expression

e.g. (2-3*4/5)^2 + 5/(4*3 -2^3)(2-3*4/5)^2 + 5/(4*3 -2^3)(2-12/5)^2 + 5/(4*3 -2^3)(2-2.4)^2 + 5/(4*3 -2^3)(-0.4)^2 + 5/(4*3 -2^3)-0.4^2 + 5/(4*3 -2^3)-0.4^2 + 5/(4*3 -8)-0.4^2 + 5/(12 -8)

-0.4^2 + 5/(4)-0.4^2 + 5/40.16 + 5/4

0.16 + 1.251.41

Page 80: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

80Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Random number

e.g.Dim ran As New Random()T1.Text = ran.Next(0, 10)

• chooses number randomly >=0 and <10 (note that 10 is not included)

• e.g.Dim ran As New Random()T1.Text = ran.Next(10, 10)

• t1.text contains 10

Page 81: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

81Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Coding a Form Load Event Procedure• Executes when the form first loads into

memory

Page 82: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

82Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Coding the btnReset_Click Event Procedure• Click the Form1.vb[Design] tab and then

double-click the btnReset control. Enter the 7 lines of code below. Do not press the ENTER key when finished

Page 83: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

83Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Using the Option Strict Statement

• Instructs Visual Basic .NET to force you to ensure that all assignment statements use the same data type on both sides of the assignment

• In the code window on line 1, type Option Strict On

Page 84: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

84Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Global Variables

Page 85: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

85Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Global Variables

• Click the end of line 195 and then press the ENTER key. Enter the line of code below, and do not press the ENTER key

Page 86: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

86Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Coding the Event Procedures for the RadioButton Controls• Enter the code below on their respective

lines:

Page 87: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

87Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Declaring Local Variables

• Click the Form1.vb[Design] tab and then double-click the btnComputePayment control. Enter the code below in the code window. Do not press ENTER after entering the last line

Page 88: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

88Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Converting Data Types

• Press the ENTER key twice. Enter the line of code below and do not press ENTER

Page 89: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

89Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Numeric Expressions and Operator Precedence

Page 90: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

90Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Construction of Error-Free Numeric Expressions

Page 91: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

91Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Coding an Expression to Calculate a Monthly Interest Rate• Press the ENTER key twice. Enter the

lines of code below and do not press the ENTER key

Page 92: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

92Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Intrinsic Functions

• Ddb()• Fv()• Lpmt()• Lrr()• Mirr()• Nper()

• Npv()• Ppmt()• Pv()• Rate()• Sin()• Syd()• pmt()

These functions are only for knowledge it is not required from you to know the detail of functions.

لالطالع فقط

Page 93: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

93Chapter 4: Working with Variables, Constants, Data Types, and Expressions

The Pmt Functionلالطالع فقط

Page 94: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

94Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Using the Pmt Function

• Press the ENTER key twice. Enter the lines of code below and do not press the ENTER key

لالطالع فقط

Page 95: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

95Chapter 4: Working with Variables, Constants, Data Types, and Expressions

The Format$ Function

Page 96: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

96Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Using the Format$ Function

• Press the ENTER key. Enter the lines of code below and do not press the ENTER key

Page 97: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

97Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Finishing the Project

• Save the project– Click the Save All button on the Standard

toolbar

• Test the project using test data and verify your output

• Document the application using your knowledge from Chapter 2

• Quit Visual Basic .NET

Page 98: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

98Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Summary

• Use the RadioButton and GroupBox controls

• Set a default button on a form

• Declare variables and constants

Page 99: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

99Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Summary

• Use variables and constants within code

• Describe Visual Basic .NET data types

• Convert between data types

• Code a form Load event procedure

• Use the Option Strict statement

Page 100: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

100Chapter 4: Working with Variables, Constants, Data Types, and Expressions

Summary

• Use arithmetic expressions

• Describe the order of operator precedence in code

• Use the Pmt function

• Use the Format$ function

Page 101: Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions

Visual Basic .NETComprehensive Concepts

and Techniques

Chapter 4 Complete