1 / 20

CS 102

CS 102. Variables/Data Types. Overview. Data Types Variables (sec 3.2) Operations on variables Numeric Text Arrays All operations have order of precedence – remember that!! 2 + 3 *4 + 2 = 16. Overview. Formatting (sec 3.5) Msgbox (section 4.8) Debugging

dareh
Download Presentation

CS 102

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. CS 102 Variables/Data Types

  2. Overview • Data Types • Variables (sec 3.2) • Operations on variables • Numeric • Text • Arrays • All operations have order of precedence – remember that!! • 2 + 3 *4 + 2 = 16

  3. Overview • Formatting (sec 3.5) • Msgbox (section 4.8) • Debugging • Using print/msgbox statements • Simple debugging/tutorial • Exception handling

  4. Data Types • Variables are always declared with a data type • Variable must follow rules for that data type • Very different than earlier versions of VB • In earlier versions, it was untyped • Basic data types: • String (Surrounded with double quotes) • “Hello world” • Boolean (True/False) • Byte (0-255) • Integer (-32,768 – 32,767) • Long (-2,147,483,648 – 2,147,483,647)

  5. Data Types • Basic data types: • Single (16 bit floating point) • Double (32 bit floating point) • Currency • Dates (Surround with # character) dtBirth = #5/1/2009# • Many, many others defined • Won’t need most/any of them for this class

  6. Variables • “Boxes” that hold data of the proper type • Declared with the “Dim” statement • Short for “Dimension” – legacy Basic command • Examples: Dim strFirstName As String Dim intLoop As Integer • “As” tells VB the data type

  7. Variables • Variable names typically start with three letters of the data type • int, str, lng… • Variable names use camel-casing • Each word starts with a capital • No spaces • Variable names are meaningful!!! • These are guidelines, but most programmers use them • Make it easier to read programs • For this class, use this convention!!

  8. Numeric Operations • Basic math functions: • sumVal = sumVal + 2 • intProd = 4 * 12 • fVal = fVal / 3 • Shorthand can combine the assignment and math • sumVal += 2 • fVal /= 3

  9. String Operations • Assign literal text with “=“ Dim strTest as string strTest = “Hello world” Msgbox(strTest) • There are a huge number of string manipulation functions available: • strTest = “Hello” & “ there” • strTest = (4 / 3).ToString() • ToString converts from the data type of the variable to a string

  10. Arrays • An array is a collection of items of the same data type • Can access the array with an integer index • Arrays are all ZERO-BASED • Array goes from 0 to index - 1 Dim strMessage(3) As String strMessage(0) = "First string" strMessage(1) = "Second string" strMessage(2) = "Third string" MsgBox(strMessage(1))

  11. Arrays • Arrays can have multiple dimensions • Creates a two-dimensional grid of values • Can have more than two dimensions Dim strPronoun(3, 2) As String strPronoun(0, 0) = "Me" strPronoun(0, 1) = "You" strPronoun(0, 2) = "He" strPronoun(1, 0) = "Moi" strPronoun(1, 1) = "Tu" strPronoun(1, 2) = "Ils" MsgBox(strPronoun(1, 2))

  12. Formatting • There are many ways to format numbers/strings • ToString (saw this before) • Can specify a format for the conversion. Otherwise it makes the best guess it can Dim dblSample As Double Dim strResult As String dblSample = 1234.5 strResult = dblSample.ToString("c") MsgBox(strResult) Answer in box is: $1234.50

  13. Formatting • Some of the formats available: • n or N (number) • f or F (floating point) • c or C (currency) • p or P (percentage) • Formatting dates/times • d – Short date format (8/2/2009) • D – Long date format (Monday, August 2, 2009) • t – Short time format (3:22 PM) • T – Long time format (2:12:34 PM) • F – Long date and time format

  14. Formatting • Format date and time example: Dim dtTest As Date = #8/23/2009# MsgBox(dtTest.ToString("D")) • Trim spaces from strings • TrimStart() • TrimEnd() • Trim() Dim strTest As String strTest = " X Hello Y " MsgBox(strTest.TrimStart) MsgBox(strTest.TrimEnd) MsgBox(strTest.Trim)

  15. Formatting • substring – returns a part of a string • substring(x) – returns the string, starting with position number x • substring(x, y) = returns the string, starting with position x, for y characters Dim strTest As String strTest = "abcdefghijklmnop" MsgBox(strTest.Substring(3)) MsgBox(strTest.Substring(4, 2))

  16. Formatting • indexOf – Searches a string for a substring and returns the posisiotn of where it is • Returns -1 if string is not found Dim strTest As String strTest = "abcdefghijklmnop" MsgBox(strTest.IndexOf("j"))

  17. Msgbox • Simple function that can be used almost anywyere • Pops up a dialog box with a string in it MsgBox(dtTest.ToString("D")) • More details in section 4.8 • Msgbox(string) – prints out the string • Msgbox(string, caption) – prints out the string, with the caption as the title of the dialogue • Msgbox(string, caption, buttons) – Same as above, but with optional buttons

  18. Msgbox • MessageBoxButtons.OK – Only the OK button • MessageBoxButtons.OKCancel – Two buttons • Other combinations in section 4-8 • Last argument is optional icon (see sec 4-8) • Example of Messagebox: Dim intRes As Integer intRes = MessageBox.Show(“Exit now?", "Exit program", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) If intRes = Windows.Forms.DialogResult.Yes Then MsgBox("Pressed Yes!") If intRes = Windows.Forms.DialogResult.No Then MsgBox("pressed No!") If intRes = Windows.Forms.DialogResult.Cancel Then MsgBox("pressed Cancel!")

  19. Exception Handling • Can be VERY complicated subject • Really useful to learn/use • Basic idea: Figure out possible problems, and handle them before they cause an unexpected error in your program • What’s wrong with this program? If IsNumeric(TextBox1.Text) Then MsgBox(TextBox1.Text) Else MsgBox("That's not a number!") End If Assume that TextBox1 is a textbox we’ve added to the form.

  20. Exception Handling • Always think about the fact that they do not have to use the program the way that you intend them to (in fact, they usually won’t). • You MUST consider: • Error conditions (ex: divide by 0) • Empty data fields (they didn’t type in data) • Boundary problems (you assume the number is between 0-9. What if they enter 22?) • Other conditions • Must program DEFENSIVELY!! • We will cover how to do that in a later lecture

More Related