1 / 18

Chapter 4

Chapter 4. Variables and constants. 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

Download Presentation

Chapter 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 -Example 12.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 4 – 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 4 – 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 2 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 = any decimal value -Integer = any whole number -String = anything containing non-numerical characters -Char = anything containing a single chararcter

  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 = 3 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. Ch 4 – Review 6 -New Project -New Form -Add 1 text box (txtSkyHooks) -Add 1 button (btnShip) -Add 4 labels -lblPrompt, lblBoxAnswer, lblBoxes -lblEnvelopeAnswer, lblEnvelopes -Add following statements to btnShip click event Dim intSkyHooks As Integer Dim intBoxes, intEnvelopes As Integer intSkyHooks = Val(Me.txtSkyHooks.text) ‘gets value from textbox intBoxes = intSkyHooks \ 3‘Calculate number of boxes intEnvelopes = intSkyHooks mod 3‘Calculate number of Envelopes Me.lblBoxAnswer.text = intBoxes‘Displays # of boxes in label Me.lblEnvelopeAnswer.text = intEnvelopes‘Displays 3 of envelops in label -Add textchanged event to clear out both labels

  15. 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

  16. 4.11 Debugging an Application -Be familiar with vocabulary from this section -Debugging -Breakpoint -Watch Window

  17. 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

  18. Pseudocode -> ACTUAL CODE radExponent_Click • What variable do you need??? (Variable Declarations) intNum1 ->Integer Dim intNum1 As Integer intNum2 -> Integer Dim intNum2 As Integer sngAnswer -> Single Dim sngAnswer As Single • What variables come from the user??? intNum1 = txtFirst intNum1 = Val(Me.txtFirst.text) intNum2 = txtSecond intNum2 = Val(Me.txtSecond.text) • What action/formula/task does the event complete??? sngAnswer = intNum1 ^ intNum2 sngAnswer = intNum1 ^ intNum2 • What is displayed and where??? lblAnswer = sngAnswer Me.lblAnswer.text = sngAnswer

More Related