Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions."— Presentation transcript:

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

2 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 2 Objectives Use the RadioButton and GroupBox controls Set a default button on a form Declare variables and constants

3 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 3 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

4 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 4 Objectives Use arithmetic expressions Describe the order of operator precedence in code Use the Pmt function Use the Format$ function

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

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

7 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 7 Setting Form Properties and Adding Controls

8 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 8 Setting Form Properties and Adding Controls Add three Label controls, two NumericUpDown controls, one TextBox control, and two Button controls

9 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 9 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

10 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 10 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

11 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 11 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

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

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

14 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 14 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

15 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 15 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

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

17 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 17 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.

18 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 18 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.

19 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 19 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,…

20 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 20 e.g. date Dim Birthday As Date Birthday = #11/27/1963# t1.text = Birthday textbox control

21 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 21 e.g. char Dim cat As String cat = “nasa" Dim ch as char ch = “a"

22 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 22 e.g. short and boolean Dim Birds As Short Birds = 12500 Dim Flag as Boolean Flag = True

23 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 23 e.g. dim x : MsgBox(x) x =20: MsgBox(x) X = “A” : MsgBox(x) X = true: MsgBox(x)

24 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 24 Dim x, x1 = 20 error

25 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 25 Declaring Constants const 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,

26 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 26 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

27 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 27 Declaring a constant The first Form: Const name As type = value The second Form: Const name = value key words

28 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 28 Declaring a constant E.g1: Const MaxVolume as Integer = 11 E.g.3: Const capacity = 150.3

29 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 29 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.

30 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 30 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.

31 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 31 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.

32 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 32 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.

33 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 33 e.g. Option strict on Public Class Form1 Inherits System.Windows.Forms.Form ' ===================== Const x As Integer = 5 ……

34 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 34 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

35 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 35 Option strict on const name = value dim var dim var = value dim var1, var2, var3,….. Error since there are no types

36 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 36 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.

37 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 37 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

38 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 38 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)

39 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 39 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 = false If 0 then z is false Any other value z is true z = CBool(-1) z = CBool(0) The same

40 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 40 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

41 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 41 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 = 35 The result k = 35 If the resulting value is greater than the maximum value that the short can save then the program will hang

42 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 42 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 = 35 Error :cannot convert from long to int

43 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 43 Option Strict On Public 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 Sub End Class Error1Option Strict On disallows implicit conversions from 'Integer' to 'String'.

44 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 44 Option Strict Off Public 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 Sub End Class

45 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 45 Option Strict On Public 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 Sub End Class

46 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 46 Option Strict On Public 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 Sub End Class Error1 Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

47 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 47 Converting Data Type Option Strict Off Public 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 Sub End Class Error: Arithmetic operation resulted in an overflow.

48 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 48 Converting Data Type Public 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.

49 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 49 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

50 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 50 option strict off Dim x As Integer = 300 Dim y As Byte y = x Error: overflow

51 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 51 option strict off Dim x As Integer = 200 Dim y As Byte y = x No error

52 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 52 Option strict off Dim x As Integer x = "23“ No error

53 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 53 Option strict off Dim x As Integer x = "23st“ error

54 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 54 Option strict off Dim x As Intege x = “A“ error

55 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 55 option strict on Dim x As Integer = 300 Dim y As Byte y = x Error1Option Strict On disallows implicit conversions from 'Integer' to 'Byte'.

56 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 56 option strict on Dim x As Integer = 200 Dim y As Byte y = x Error1Option Strict On disallows implicit conversions from 'Integer' to 'Byte'.

57 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 57 option strict on Dim x As Integer = 200 Dim y As Byte x = y No error

58 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 58 Option strict on Dim x As Integer x = "23“ Error1Option Strict On disallows implicit conversions from 'String' to 'Integer'.

59 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 59 Option strict on Dim x As Integer x = "23st“ Error1Option Strict On disallows implicit conversions from 'String' to 'Integer'.

60 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 60 Option strict on Dim x As Integer x = “A“ ErrorOption Strict On disallows implicit conversions from 'String' to 'Integer'.

61 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 61 Option strict off Dim x As Integer x = 3 + "6" + 2 MsgBox(x) The result is x = 11

62 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 62 Option strict on Dim x As Integer x = 3 + "6" + 2 MsgBox(x) error Option Strict On disallows implicit conversions from 'String' to 'Double'.

63 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 63 Option Strict Off Dim x As Integer x = 3 + "6kk" + 2 MsgBox(x) error Conversion from string "6kk" to type 'Double' is not valid.

64 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 64 Option Strict Off Dim x As String x = 3 + "6" + 2 MsgBox(x) The result is x = 11

65 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 65 Option Strict On Public 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 Sub End Class Error1Option Strict On disallows implicit conversions from 'String' to 'Double'.

66 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 66 Option strict off Dim x As Integer x = 3.5 MsgBox(x) The result is x = 4

67 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 67 Exercise: try the following Option Strict Off Option Strict On Dim x As Integer x = 3.5 MsgBox(x)

68 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 68 Option strict on Dim x As Integer x = 3.5 MsgBox(x) Error1Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

69 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 69 Option Strict On Dim x As Integer x = 4 Mod 3 MsgBox(x) The result is x =1

70 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 70 Option Strict On Dim x As Integer x = 2 + 5 * 2.0 MsgBox(x) Error1Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

71 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 71 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.

72 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 72 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.

73 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 73 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

74 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 74 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).

75 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 75 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

76 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 76 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: -

77 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 77 The precedence in the numeric expression E.g. 18/3 ^ 2 + 4 * 2 18/9 + 4 * 2 2 + 4 * 2 2 + 8 10

78 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 78 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\2.4-3 27 mod 4 +2-3 3 + 2 -3 5 -3 2

79 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 79 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/4 0.16 + 5/4 0.16 + 1.25 1.41

80 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 80 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

81 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 81 Coding a Form Load Event Procedure Executes when the form first loads into memory

82 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 82 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

83 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 83 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

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

85 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 85 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

86 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 86 Coding the Event Procedures for the RadioButton Controls Enter the code below on their respective lines:

87 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 87 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

88 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 88 Converting Data Types Press the ENTER key twice. Enter the line of code below and do not press ENTER

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

90 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 90 Construction of Error-Free Numeric Expressions

91 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 91 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

92 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 92 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. للاطلاع فقط

93 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 93 The Pmt Function للاطلاع فقط

94 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 94 Using the Pmt Function Press the ENTER key twice. Enter the lines of code below and do not press the ENTER key للاطلاع فقط

95 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 95 The Format$ Function

96 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 96 Using the Format$ Function Press the ENTER key. Enter the lines of code below and do not press the ENTER key

97 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 97 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

98 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 98 Summary Use the RadioButton and GroupBox controls Set a default button on a form Declare variables and constants

99 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 99 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

100 Chapter 4: Working with Variables, Constants, Data Types, and Expressions 100 Summary Use arithmetic expressions Describe the order of operator precedence in code Use the Pmt function Use the Format$ function

101 Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Complete


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

Similar presentations


Ads by Google