Download presentation
Presentation is loading. Please wait.
Published byBuddy Clarke Modified over 9 years ago
1
3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic
2
3-2 Learning Objectives Understand a four-step process to writing code in VB.NET and its relationship the six operations a computer can perform Declare and use different types of variables in your project Describe the various type of data used in a program Use text boxes for event-driven input and output
3
3-3 Learning Objectives (continued) Write VB.NET functions to carry out commonly used operations Use buttons to clear text boxes and exit the project Describe the type of errors that commonly occur in a VB.NET project and their causes
4
3-4 Four step coding process Once the logic is defined through IPO tables or pseudocode, one needs to: Decide what variables and constants will be needed and declare them Input data from text boxes or other input controls Process data into information using arithmetic or string operations and store results in variables Output values of variables to text boxes or other output controls
5
3-5 Variables Conceptually a variable is a space in memory with a Name and a Value. The name identify the variable itself. It does not change. The value can be changed at will, i.e. it can be written to as well as read from.
6
3-6 Naming Variables A variable name can contain – Letters, A-Z – Digits, 0-9 – The underscore _ A variable name must start with a letter Variable names are case insensitive – MyVariable and myvariable refer to the same variable
7
3-7 Variable Names Cannot be any of VB.NET keywords Preferably meaningful names Often follows some convention to indicate – The type of the variable – The scope of the variable – Perhaps other characteristic
8
3-8 Commonly Used Data Types String : to hold text Single : a real number Double : a real number with larger coverage and precision than the single Integer : an integer Long : an integer with larger coverage Decimal : to work with currency Date : a date Boolean : True or False
9
3-9 Data types
10
3-10 Declaring Variables Purpose : informing VB.NET of the type of the variable Typical declaration: – Dim variableName As variable Type Examples – Dim dblWeight As Double – Dim strName As String – Dim x As Long, y As Long
11
3-11 Declare two variables of same type – Dim x, y As Long – Dim c As Integer, x, y As Long – In both cases, both x and y are Long Initialization and declaration – Dim intAge As Integer = 10 – Dim decPrice As Decimal = 100.00
12
3-12 Option Explicit Statement Use one of the following to enforce the requirement of declaring every variable you use (or not) – Option Explicit On – Option Explicit Off By default Option Explicit is On The Statement should be at the very top of the module
13
3-13 Event-Driven Input You need data to operate on You need a way to “input” the data You typically use controls such as text boxes to take the input You read the value of the input in code when a certain event occurs
14
3-14 TextBox Control Allows the user to enter text Can also be used to display text The Text property of the control is always equal to the text entered in the box
15
3-15 A simple calculator A simple application where – User enters two numbers in TextBoxes – User clicks a Sum button – The application displays the sum in another TextBox
16
3-16 Calculator appearance
17
3-17 Control properties
18
3-18 Assignment Statement Declare variables to hold useful values – Dim intFirst, intSecond, intSum As Integer When user clicks the sum button, read and assign the values in each TextBox to the right variable – intFirst = txtFirstNum.Text – intSecond = txtSecondNum.Text Assignment : VariableName = SomeValue
19
3-19 Tip: Tab Order A power user can use the Tab key to navigate controls in an application The order of the controls depends on the order in which they are laced on the form To change the default order – Change the TabIndex property of controls – Use the View | Tab Order menu
20
3-20 Functions A function is a black box taking some input (arguments) and returning a value
21
3-21 Example of Functions CInt() : converts the argument to integer Today() : returns the system date CStr() : converts a number to a string – Dim MyString as String, MyInt as Integer = 12 – MyString = CStr( MyInt) – MyString holds the value “12”, not 12
22
3-22 Code for btnSum Private Sub btnSum_Click(ByVal sender as System.Object, _ ByVal e as System.EventArgs) _ Handles btnSum.Click Dim intFirst, intSecond, intSum As Integer intFirst = txtFirstNum.Text intSecond = txtSecondNum.Text intSum = intFirst + intSecond txtSum.text = str(intSum) End Sub
23
3-23 Properties and Methods Access properties and methods through the dot notation – object.propertyName – object.methodName Ex: The Focus method shifts the cursor to the object calling it – txtFirstNum.Focus()
24
3-24 Step-By-Step 3-1 Creating calculator Demo
25
3-25 Arithmetic Operators
26
3-26 Hierarchy of operations The order in which operations are executed is the following 1. Parentheses 2. Raising to a power 3. Change of sign (negation) 4. Multiplication or division 5. Integer division 6. Modulus 7. Addition or subtraction
27
3-27 String Operators The only string operator is the concatenation operator, & It combines two strings into one – Dim strFName as String = “Jesse” – Dim strLName as String = “James” – Dim strName as String – strName = strFName & “ “ & strLName
28
3-28 Symbolic Constants Rather than have “magic” numbers in your programs, use symbolic constants Ex: you want to find the total sale when adding sales tax (now 7%) Declare a Constant and use it in code – Const TaxRate as Double = 0.07 If you maintain the program later and the tax rate has changed you need to reflect the change in a single place
29
3-29 Vintage DVDs Extend application. Allow to input renter’s name, the DVD rented, and price for DVD. Allow for printing rental price with tax, clearing the textboxes, and exiting application.
30
3-30 Step-by-Step 3-2 Modifying a project Demo
31
3-31 Develop logic for action objects
32
3-32 Write and test code Code for Calculate button Private Sub btnCalc_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCalc.Click Const sngTaxRate As Single = 0.07 'Use local tax rate Dim decPrice, decAmountDue, decTaxes As Decimal decPrice = CDec(txtDVDPrice.Text) decTaxes = decPrice * sngTaxRate 'Compute taxes decAmountDue = decPrice + decTaxes 'Compute amount due txtTaxes.Text = CStr(decTaxes) txtAmountDue.Text = CStr(decAmountDue) End Sub
33
3-33 Conversion functions
34
3-34 Formatting Output Formatting functions – Format( expression, format ) – FormatCurrency( expression )
35
3-35 Format Expressions
36
3-36 Clearing Entries on the Form Private Sub btnClear_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) _ Handles btnClear.Click txtCustName.Text = "" txtDVDName.Text = "" txtDVDPrice.Text = "" txtTaxes.Text = "" txtAmountDue.Text = "" txtCustName.Focus() End Sub
37
3-37 Step-by-Step 3-3 Adding controls to Vintage DVDs Demo
38
3-38 More built-in functions
39
3-39 Monthly payment Calculator
40
3-40 Code for btnCompute_Click Private Sub btnCompute_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) _ Handles btnCompute.Click Dim decAmount, decPayment As Decimal Dim intMonths As Integer Dim sngRate As Single decAmount = CDec(txtAmount.Text) intMonths = CInt(txtMonths.Text) sngRate = (CSng(txtRate.Text) / 100) / 12 decPayment = Pmt(sngRate, intMonths, -decAmount) txtPayment.Text = Format(decPayment, "currency") txtAmount.Text = Format(decAmount, "currency") End Sub
41
3-41 Ste-by-Step 3-4 Creating Monthly Payment Calculator Demo
42
3-42 Errors in VB.Net Syntax Errors – Incorrect grammar – Incorrect Usage Runtime Errors – Occurs while program is running – Caused by un planned conditions division by 0 Logic Errors – Incorrect program design
43
3-43 Copyright 2004 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.