Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable must be on the left and the expression on the right.
Chapter 3 - Visual Basic Schneider Assignment Statement: The statement var = expr assigns the value of the expression to the variable tax = 0.02 * (income * dependents) sum = 2 + x y
Chapter 3 - Visual Basic Schneider Valid Numeric Variable Names: timeElapsed taxRate speed n celsius
Chapter 3 - Visual Basic Schneider Invalid Numeric Variable Names: maximum/average 1stChoice square yard
Chapter 3 - Visual Basic Schneider Valid Assignment Statements count = count + 1 num = 5 count = count + num /2
Chapter 3 - Visual Basic Schneider Invalid Assignment Statements 10 = count count + 1 = count
Chapter 3 - Visual Basic Schneider Visual Basic Print Statement Print is a method used to display data on the screen or printer. Can be used to display values of variables or expressions
Chapter 3 - Visual Basic Schneider Examples of Print Statements Private Sub cmdCompute_Click() picResults.Print picResults.Print picResults.Print 3 * 2 picResults.Print 3 / 2 picResults.Print 3 ^ 2 picResults.Print 2 * (3 + 4) End Sub
Chapter 3 - Visual Basic Schneider Examples of Print Statements picOutput.Print speed picOutput.Print taxRate picOutput.Print “Class average is”; total / 3
Chapter 3 - Visual Basic Schneider Examples of Print Statements x = 15 y = 5 picOutput.Print (x + y) / 2, x / y Output: 103
Chapter 3 - Visual Basic Schneider String Constants: A sequence of characters treated as a single item The characters in a string must be surrounded by double quotes (“ ”)
Chapter 3 - Visual Basic Schneider Valid String Constants “A rose by any other name” “9W” “134.23” “She said, ‘stop, thief!’ ”
Chapter 3 - Visual Basic Schneider Invalid String Constants ‘Down by the Seashore’ “ “She said, “Stop, thief!””
Chapter 3 - Visual Basic Schneider String Variables A string variable stores a string. The rules for naming string variables are identical to those for naming numeric variables. When a string variable is first declared, its value is the empty string.
Chapter 3 - Visual Basic Schneider String Variable Example Private Sub cmdShow_Click() picOutput.Cls phrase = "win or lose that counts." picOutput.Print "It's not whether you "; phrase picOutput.Print "It's whether I "; phrase End Sub
Chapter 3 - Visual Basic Schneider Concatenation Two strings can be combined by using the concatenation operation. The concatenation operator is the ampersand (&) sign.
Chapter 3 - Visual Basic Schneider Examples of Concatenation: strVar1 = “Hello” strVar2 = “World” picOutput.Print strVar1& strVar2 txtBox.Text = “32” & Chr(176) & “ Fahrenheit”
Chapter 3 - Visual Basic Schneider Declaring Variable Types Use the Dim statement to declare the type of a variable. Examples: Dim number As Integer Dim flower As String Dim interestRate As Single