1 / 19

CECS 5020 Computers in Education

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.

ingo
Télécharger la présentation

CECS 5020 Computers in Education

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. CECS 5020Computers in Education Visual Basic Variables and Constants

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

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

  4. Byte Integer Long Single Double Currency Dec ^ Exponentiation - Negation * Multiplication / Division \ Integer division + Addition - Subtraction Mod Modulo Numeric Variables and Operators

  5. 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”)

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

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

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

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

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

  11. 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.)

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

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

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

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

  17. Comparison Operators • = Equality • <> Inequality • < Less Than • > Greater Than • <= Less Than or Equal • >= Greater Than or Equal

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

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

More Related