Download presentation
Presentation is loading. Please wait.
Published byAlfred Sparks Modified over 9 years ago
1
CECS 5020 Computers in Education Visual Basic Variables and Constants
2
CECS 5020 Variables u Variables are containers or place holders u Variables store different types of data u Data = Numbers, Characters, Strings u Assign variables using the equal operator 0 = 5 26 5 “Let the variable counter have the value of 5.” After the assignment, the previous value of counter is destroyed counter
3
CECS 5020 Declaring Variables u Declaring a variable tells VB the variable’s type u It’s good practice to insert the command “Option Explicit” in the General section of your code to remind you to declare all variables u Variables are declared with the “Dim” (dimension) command. Examples: Dim num1 as Integer Dim str1 as String
4
CECS 5020 Numeric Variables and Operators u Byte u Integer u Long u Single u Double u Currency u Dec u ^ Exponentiation u - Negation u * Multiplication u / Division u \ Integer division u + Addition u - Subtraction u Mod Modulo
5
CECS 5020 String Variables and Operators u Variable-length (up to 2 billion bytes) u Fixed-length (up to 64,000 bytes) u You usually use variable-length strings in your code u Operators: & Concatenation (“to gether together”)
6
CECS 5020 Other Variable Types u Boolean (True or False, Yes or No) u Boolean operators: –AND –OR –NOT u Date (Between Jan. 1, 100 and Dec. 31, 9999). Also stores hours and seconds.
7
CECS 5020 Useful Date Functions u Now() - Returns current date and time u Date - Returns current date u Time - Returns current time u IsDate(string) - Returns a Boolean saying whether string is a valid date u DateValue(date) - Returns a Date value from a string in date form u Weekday(date) - Returns the serial number of the day of the week of date
8
CECS 5020 The Immediate Window and Debug.Print u Debug.Print “prints” to the immediate window u Useful for checking (“debugging”) your code u Example: Debug.Print “The value of icount is “ & icount
9
CECS 5020 Useful String Functions u Len() - Returns the length of a string u InStr() - Searches for a substring within a string u Left(), Right(), Mid() - Return portions of a string from the left, right, or middle of the string
10
CECS 5020 Useful String Functions u Ltrim(), Rtrim(), Trim() - Remove spaces from the left, right, or both ends of strings u Chr() - Returns the ASCII character corresponding to a numeric value u StrComp() - Compares two strings
11
CECS 5020 Commenting Your Code u Use the ‘ (apostrophe) to start a comment u Anything after the ‘ is not executed u Comments promote readability, user understanding u Use them whenever something needs to be explained (not “assign the variable”, etc.)
12
CECS 5020 Immediate Execution u Start a new project and add “Option Explicit” and “Dim icount as integer” to the General section of the code u Add “count = 1001” to the Load event of the form u Add a button with the caption “Test” u Run the program
13
CECS 5020 Immediate Execution u Use the “pause” button on the VB tool bar to interrupt your code u In the Immediate window, enter “Print icount” and press enter u Enter “icount = icount + 2”, and print icount again
14
CECS 5020 Program Flow and the If statement u VB code is executed from top to bottom unless something re-directs the flow u The If statement makes decisions about program flow and execution u Syntax: If [expression] then Statement(s) [Else Statement(s)] End If
15
CECS 5020 Example of an If Statement If MyTxtBox.Text = “Yes” Then count = count + 3 MsgBox “The count was increased” Else count = count - 3 MsgBox “The count was decreased” End If
16
CECS 5020 Nesting If Statements u You can “nest” if statements: If count <= 0 Then If count > -1000 Then MsgBox “Count is between 0 and -1000” Else MsgBox “Count is less than -1000” End If Else MsgBox “Count is greater than 0” End If
17
CECS 5020 Comparison Operators u = Equality u <> Inequality u < Less Than u > Greater Than u <= Less Than or Equal u >= Greater Than or Equal
18
CECS 5020 Constants u Constants allow you to use mnemonic names for values that never change u They are declared like variables, except with the Const statement: Const count = 1001 Const sMyName = “John James Harrison” u Use constants to clarify your code u VB has a number of supplied constants: see them in the Object Browser
19
Sample Code u Add three TextBoxes to the form, naming them “First”, “Second”, and “Result” u Replace the button code with: ‘ Simple test of which of two strings is longer and puts result in the ‘ Result textbox If Len(First.Text) > Len(Second.Text) then If Left(First.Text,1) >= “A” and Left(First.Text,1) <= “Z” Then Result.Text = “First is longer: starts with Upper case” Else Result.Text = “First is longer: doesn’t start with upper case” End If Else Result.Text = “Second is longer” End If
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.