Upload
phyllis-ford
View
230
Download
0
Embed Size (px)
Citation preview
Visual Basic .NETComprehensive 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
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
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
5Chapter 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
7Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Setting Form Properties and Adding Controls
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
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
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
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
12Chapter 4: Working with Variables, Constants, Data Types, and Expressions
GroupBox Control Properties
13Chapter 4: Working with Variables, Constants, Data Types, and Expressions
RadioButton Control Properties
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
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
16Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Data Types
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.
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.
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,…
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
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"
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
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)
24Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Dim x, x1 = 20
error
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,
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
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
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
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.
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.
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.
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.
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
……
……
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
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
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.
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
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)
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
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
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
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
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'.
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
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
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'.
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.
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.
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
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
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
52Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Option strict off
Dim x As Integer
x = "23“
No error
53Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Option strict off
Dim x As Integer
x = "23st“
error
54Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Option strict off
Dim x As Intege
x = “A“
error
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'.
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'.
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
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'.
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'.
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'.
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
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'.
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.
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
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'.
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
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)
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'.
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
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'.
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.
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.
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
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).
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
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: -
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
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
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
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
81Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Coding a Form Load Event Procedure• Executes when the form first loads into
memory
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
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
84Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Declaring Global Variables
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
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:
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
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
89Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Numeric Expressions and Operator Precedence
90Chapter 4: Working with Variables, Constants, Data Types, and Expressions
Construction of Error-Free Numeric 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
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.
لالطالع فقط
93Chapter 4: Working with Variables, Constants, Data Types, and Expressions
The Pmt Functionلالطالع فقط
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
لالطالع فقط
95Chapter 4: Working with Variables, Constants, Data Types, and Expressions
The Format$ Function
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
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
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
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
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
Visual Basic .NETComprehensive Concepts
and Techniques
Chapter 4 Complete