Download presentation
Presentation is loading. Please wait.
Published bySara Dickerson Modified over 9 years ago
1
Chapter 4 Variables and constants
2
4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand -Declaration Statement (variables must be declared before you can use them) -Format: Dim variable_name As type -Examples: Dim sngLength As Single Dim sngWidth As Single -Single (Data type) -any numeric value that can contain a decimal -variable name begins with sng -Examples:sngLength, sngWidth, sngHeight -Initializing a value (not necessary but helpful) - Example:Dim sngLength As Single = 12.3 -Multiple Declarations in a single statement (not necessary but helpful) - Example:Dim sngRadius, sngDiameter As Single
3
4.2 Variable Assignments -A value stored in a variable can be changed at run-time through assignment -Format: variable_name = value -Example:Dim sngRadius As Single = 10‘declaration and initialization sngRadius = 12.3‘change value -Using variables to write formulas -Example:sngCircleArea = 3.14 * sngRadius ^ 2 ‘sngCircleArea = 475.06 -Common Error -Example12.3 = sngRadius(What’s wrong???) -Think About it -Example:Dim sngX As Single sngX = 5.5 sngX = 8 Me.lblResult.text = sngX(What will be displayed???)
4
Ch 3 – Review 1 -New Project -New Form -Add 2 labels (lblQuestion, lblAnswer) -Add 1 button (btnAnswer) -Add appropriate comments to your code -Add btnanswer click event -Add following statements to event procedure Dim sngRadius As Single = 6 Dim sngCircleArea As Single sngCircleArea = 3.14 * sngRadius ^ 2‘area of circle formula Me.lblAnswer.text = sngCircleArea‘Displays answer in label -Run program (Answer should be 113.04)
5
4.3 Obtaining a Value from the User -TextBox Object (Allows users to enter text at run-time) -Properties: -Name: txt + description -Text: What is displayed (usually left blank) -TextAlign:Sets alignment of textbox -Prompt (A label that is placed near a text box to describe its contents/purpose) -TextChanged Event -Used to make changes when a user changes the text in a textbox -Anything typed in the textbox is stored in the text property for the textbox -Example:sngRadius = Me.txtRadius.text -If the textbox contains a number (Code as Follows!!!) -sngRadius = Val(me.txtRadius.text)‘converts text to number -What will sngHeight be assigned in the following examples??? 1. sngHeight = Val(“62.5 inches”) 2. sngHeight = Val(“Twenty Inches”) 3. sngHeight = Val(“six feet 2 inches”)
6
Ch 3 – Review 2 -New Project -New Form -Add 2 labels (lblPrompt, lblAnswer) -Add 1 text box (txtRadius) -Add 1 button (btnAnswer) -Add btnAnswer click event -Add following statements to event procedure Dim sngRadius As Single Dim sngCircleArea As Single sngRadius = Val(Me.txtRadius.text)‘gets value from textbox sngCircleArea = 3.14 * sngRadius ^ 2‘area of circle formula Me.lblAnswer.text = sngCircleArea‘Displays answer in label -Run program (Enter 3 into text box. Answer should be 12.56)
7
4.4 Using Named Constants -A constant is a named memory location which stores a value that can’t be changed. -Constant Declarations: -Format:Const variable_name As Single = Value -Example:Const sngPI As Single = 3.14159 -Good Programming Style -Description part of name is all capitals -Place before variable declarations -Examples: sngPI, sngSPEEDOFLIGHT -Common Error (trying to change value of constant) Const sngPI As Single = 3.14159 sngPI = 22/7
8
4.5 Choosing Identifiers -Legal Names for variables and constants -must begin with a letter -can only contain letters, digits, and the underscore character -periods, spaces, and other special characters not allowed -can’t be a keyword (any word used by VB) -Show up in a different font than normal code -Examples: Single, End, Sub, Private, etc.
9
4.6 Built – In Data Types -Refer to (Visual Aide p. 17) -Special Considerations: -Char % String assignments must be in quotation marks -Initialization Value -Short, Integer, Long, Single, Double, Decimal are set to 0 -Date set to 12:00:00 AM -Boolean set to FALSE -Char, String set NOTHING -NOTHING is the same as “ “ -Example: me.lblMember5.text = NOTHING -Most common types to be used in class -Single = and decimal value -Integer = any whole number -String = and thing containing non-numerical characters
10
4.7 Automatic Type Conversion -VB will convert decimals to Integers following normal rounding rules -Example: Dim intX, intY As Integer intX = 6.7 intY = 12.3 What value is stored in intX and intY???
11
4.8 Scope -Scope explains which procedures can use a variable -Local Scope = variable declared inside of event procedure -only that procedure can use variable -Global Scope = variable declared outside of event procedure -all procedures can use variable -Visual Aide (p. 18) -Good Programming Style = only use global variables if completely necessary
12
Ch. 4 – Review 5 -New Project -New Form -Add 2 labels (lblPrompt, lblAnswer) -Add 2 buttons (btnProc1, btnProc2) -Add Comment Code -Add Global Variable Dim intX As Integer = 10 -Add btnProc1 Click Event Dim intX As Integer = 10 Me.lblAnswer.Text = intX -Add btnProc2 Click Event Me.lblAnswer.Text = intX -Run Program (Why does the Answer change???)
13
4.9 Special Division Operators -Integer Division ( \ ) -divides 2 numbers, but gives answer as a whole number -Example: How many basketball teams can we make with 23 people? 23 \ 5 = 4 teams -Modulus Division ( mod ) -divides 2 numbers, but gives answer as the remainder -Example: How many leftover people after we make full teams with 23 people? 23 mod 5 = 3 people -Order of Operations - Parentheses, Exponents, Multiply, Divide, Integer division, Modulus, Add, Subtract
14
4.10 Programming Errors -Syntax Error (violates rules of VB.NET) -not syntactically correct -usually underlined with wavy blue line -Logic / Semantic Error (syntactically correct but produces undesired results) -Hard to detect -Examples: wrong formulas, wrong data types, etc. -Computer will not detect, programmer must find through good testing -Run-time Error (halts program during testing) -sometimes called an exception -will display a message box with what caused the error
15
4.11 Debugging an Application -Be familiar with vocabulary from this section -Debugging -Breakpoint -Watch Window
16
Ch. 4 – Case Study -Sketch Form -Use Arrows to name all objects -Fill out Initial Properties table -Brief description of whole project -Write PseudoCode for each event procedure -Should include needed variables, input, operation, and output
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.