190 likes | 221 Views
CECS 5020 Computers in Education. Visual Basic Variables and Constants. Variables. Variables are containers or place holders Variables store different types of data Data = Numbers, Characters, Strings Assign variables using the equal operator. counter. 5.
E N D
CECS 5020Computers in Education Visual Basic Variables and Constants
Variables • Variables are containers or place holders • Variables store different types of data • Data = Numbers, Characters, Strings • Assign variables using the equal operator counter 5 “Let the variable counter have the value of 5.” = 0 After the assignment, the previous value of counter is destroyed 26 5
Declaring Variables • Declaring a variable tells VB the variable’s type • It’s good practice to insert the command “Option Explicit” in the General section of your code to remind you to declare all variables • Variables are declared with the “Dim” (dimension) command. Examples: Dim num1 as Integer Dim str1 as String
Byte Integer Long Single Double Currency Dec ^ Exponentiation - Negation * Multiplication / Division \ Integer division + Addition - Subtraction Mod Modulo Numeric Variables and Operators
String Variables and Operators • Variable-length (up to 2 billion bytes) • Fixed-length (up to 64,000 bytes) • You usually use variable-length strings in your code • Operators: & Concatenation (“to gether together”)
Other Variable Types • Boolean (True or False, Yes or No) • Boolean operators: • AND • OR • NOT • Date (Between Jan. 1, 100 and Dec. 31, 9999). Also stores hours and seconds.
Useful Date Functions • Now() - Returns current date and time • Date - Returns current date • Time - Returns current time • IsDate(string) - Returns a Boolean saying whether string is a valid date • DateValue(date) - Returns a Date value from a string in date form • Weekday(date) - Returns the serial number of the day of the week of date
The Immediate Window and Debug.Print • Debug.Print “prints” to the immediate window • Useful for checking (“debugging”) your code • Example: Debug.Print “The value of icount is “ & icount
Useful String Functions • Len() - Returns the length of a string • InStr() - Searches for a substring within a string • Left(), Right(), Mid() - Return portions of a string from the left, right, or middle of the string
Useful String Functions • Ltrim(), Rtrim(), Trim() - Remove spaces from the left, right, or both ends of strings • Chr() - Returns the ASCII character corresponding to a numeric value • StrComp() - Compares two strings
Commenting Your Code • Use the ‘ (apostrophe) to start a comment • Anything after the ‘ is not executed • Comments promote readability, user understanding • Use them whenever something needs to be explained (not “assign the variable”, etc.)
Immediate Execution • Start a new project and add “Option Explicit” and “Dim icount as integer” to the General section of the code • Add “count = 1001” to the Load event of the form • Add a button with the caption “Test” • Run the program
Immediate Execution • Use the “pause” button on the VB tool bar to interrupt your code • In the Immediate window, enter “Print icount” and press enter • Enter “icount = icount + 2”, and print icount again
Program Flow and the If statement • VB code is executed from top to bottom unless something re-directs the flow • The If statement makes decisions about program flow and execution • Syntax: If [expression] then Statement(s) [Else Statement(s)] End If
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
Nesting If Statements • 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
Comparison Operators • = Equality • <> Inequality • < Less Than • > Greater Than • <= Less Than or Equal • >= Greater Than or Equal
Constants • Constants allow you to use mnemonic names for values that never change • They are declared like variables, except with the Const statement: Const count = 1001 Const sMyName = “John James Harrison” • Use constants to clarify your code • VB has a number of supplied constants: see them in the Object Browser
Sample Code • Add three TextBoxes to the form, naming them “First”, “Second”, and “Result” • 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