1 / 36

Variables & Function Calls

Variables & Function Calls. Overview. Variables Programmer Defined & Intrinsic Data Types Calculation issues Using Functions The val() function The msgbox() function. Types of Data. Numeric Can contain only numbers Real numbers, integers, currency, dates

duard
Download Presentation

Variables & Function Calls

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. Variables & Function Calls

  2. Overview • Variables • Programmer Defined & Intrinsic • Data Types • Calculation issues • Using Functions • The val() function • The msgbox() function

  3. Types of Data • Numeric • Can contain only numbers • Real numbers, integers, currency, dates • Used for arithmetic calculations • String (text) • May contain any symbol • No arithmetic calculations • Append, insert, extract

  4. Variables - The Concept • Variables are named containers of data • They are not displayed on a form • The contents can change during runtime • Variables vary in size and purpose • Some types of data require more memory than others • Each variable belongs to a type • The type of a variable determines its size, and what operations (such as add, substract, append) will work on the variable

  5. VB Variables • Variables are not created using the tool box • Instead, they are declared within the program • Declaring a variable • sets aside memory for the variable • assigns a name to that space in memory • describes what type of data can be stored there • Variables don’t work with the properties window • Changes to a variable must be made in a program

  6. Declaring a Variable • A declaration begins with the Dim statement • This alerts VB that a new variable is being defined • A declaration contains a variable name and type • Just as each control must have a (name), like lblInput, and type, like label • The form of a declaration: Dim <name> as <type> • For example: Dim intCount as Integer

  7. Numeric Variable Types • There are several different numeric variable types, each with varying • Precision • Size • Representation scheme • Selecting the wrong type for variables can hurt the performance of a program

  8. Counting Things • Integer types • BYTE  Small range of values, 0 to 255 • INTEGER  -32,768 to +32,767 • LONG  -2,147,483,648 to +2,147,483,647

  9. Measuring Things • SINGLE • Positive or negative • As close to zero as 1.401298E-45 • As large as 3.402823E38 • DOUBLE • Positive or negative • As close to zero as 4.94065645841247E-327 • As large as 1.79769313486232E308

  10. Highest Precision • Scaled integers • CURRENCY • +/-922,337,203,685,477.5807 • DECIMAL • +/-79,228,162,514,262,337,593,543,950,335 • +/-7.9228162514262337593543950335 • no rounding, slow but sure

  11. Storage Space & Prefixes • BYTE 1 byte byt • INTEGER 2 bytes int • LONG 4 bytes lng • SINGLE 4 bytes sng • DOUBLE 8 bytes dbl • BOOLEAN 2 bytes bln • CURRENCY 8 bytes cur • DECIMAL 14 bytes dec

  12. Establishing Values Dim intValue as integer intValue = 12 intValue = ”12” intValue = 12.5 intValue = 11.51 Due to automatic type conversion, all result in the integer variable intValue being set to 12

  13. Operators & Precedence 1. ^ Exponentiation 2. - Unary Negation 3. * / Multiplication and Division 4. \ Integer Division 5. MOD  Remainder 6. + - Addition and Subtraction (otherwise left to right)

  14. Precedence Examples sngA = ( 3 + 4 ) / ( 2 - 3 ) results: sngA = -7.00 sngB = ( 3 + 4 ) / 2 - 3 results: sngB = 0.50 sngC = 3 * 4 / 2 * 3 results: sngC = 18.00 sngD = ( 3 * 4 ) / ( 2 * 3 ) results: sngD = 2.00

  15. Real & Integer Arithmetic sngA = 5 / 2 results:sngA = 2.50 sngB = 5 \ 2 results:sngB = 2.00 intC = 5 MOD 2 results: intC = 1 intD = 5 / 2 results: intD = 2

  16. Text Strings • Used to hold an arbitrary list of characters • The size can be predetermined, or allowed to change as the program runs • The .caption property of a label is a string, as is the text property of a textbox

  17. String Variables Variable and fixed length strings: Dim strNamer As String Dim strMiddle As String * 1 Dim strState As String * 2 strNamer = ”Jason Ogelthorpe” strMiddle = ”P” strState = ”WI”

  18. String Variables Dim strSocSoc As String * 11 Dim strZipCode As String * 10 strSocSoc = ”128-24-1234” strZipCode = ”53211” (strZipCode = ”53211 ”) (note |1234567890|)

  19. Concatenation of Strings strSamp = ”Hi” & ” There” (strSamp = ”Hi There”) strSamp = ”Value” & 27 (strSamp = ”Value27”) strSamp = ”Value” & str(27) (strSamp = ”Value 27”)

  20. Long Strings strSamp = ”We can build ” _ & ”a very, very, very, ” _ & ”long string.” (a concatenated string can be split over several lines, note underscore characters and note spaces contained within the quoted areas.)

  21. Naming VB Variables • Programmer defined • Don’t use reserved words • Type names, control names, keywords (DIM or end) • Make the name meaningful • intNum vs intNumberOfGroceries • Capitalize first character after prefix • If multiple words, capitalize the 1st character of each • use standard prefixes

  22. Naming VB Variables • Are these valid names? • int Number • Sub • MinimumRate • Caption • CarCount • intNumberofcars

  23. VB Constants • Types of Constants • Literals • sngTaxRate = .075 • txtName = “Smith” • Symbolic • programmer defined with Const keyword const <name> as <type> = <value> Const sngTaxRate as Single = .075 • Some are defined by VB • vbRed • vbCenter • What are benefits of symbolic constants? • Why avoid literals in code?

  24. Working with Variables • Declaring Variables • Dim statement, short for Dimension, • Sets aside memory • Labels the memory • Initializes variable based upon type • Dim intPhone as Integer • Creates variable that can store an integer number • Dim strLastName as String • Creates variable that can store a string (of variable length) • Dim strLastName as String * 5 • Creates variable that can store a string of 5 characters.

  25. Working with Variables • Assignment Statement • Each statement in Experiment 3 was an assignment statements • Basic format • <name> = <expression> • Use an = (equals sign – read “is assigned”) • VB evaluates the expression to the right of the equal sign • If necessary, that value is converted so that it has the same type as the variable • The value is copied in to the variable – old data is overwritten • For Numeric Data Types • A = B + 8*C (Variable A is assigned the value of the resolved expression) • For String Data Types • strFull = strFirst & strLast (The right side is concatenated and assigned into strFull)

  26. Working with Variables • Control Properties are Variables • lblName.Caption = “My label” • Type Mismatch intNumber = strLastName • Misspellings • Use OPTION EXPLICIT statement to make the computer find errors • If a variable used in an assignment is not in a DIM statement, then VB does not allow the program to compile

  27. Using Functions • Some activities are common in programming tasks • Converting a string to a number • Pop up a warning message for the user • Extracting a substring from a string • Rather than forcing programmers to re-write the code which handles these activities each time they are needed, we can write one reusable function • Functions have names, like variables, and parameter lists

  28. The form of a function call • The general form of a function call is • <function name> ( <parameter 1>, <parameter 2>, … ) • Functions take input values, called parameters, and produce an output value which can be assigned to a variable • dblValue = squareRoot(intCount) • Visual Basic includes many functions we can use • We will learn how to create our own functions • Chapter 6

  29. The VAL Function • This function allows conversion between string filled variables and numeric variables. • It works by “scanning” the characters of the string filled variable and builds a numeric value until the end-of-string or a non numeric is found.

  30. A Simple Program Private Sub cmdComnd_Click() Label1.Caption = Val(Text1.Text) End Sub We can enter character strings into the box “Text1”, press the command button, and look at what appears in the box “Label1”.

  31. Some Examples

  32. The Message Box • VB allows the user to generate message boxes as a part of a user program. When invoked they look something like this:

  33. Format of MsgBox • In its simplest form the MsgBox is called by this command: Dim intX As Integer intX = MsgBox("Read the Password")

  34. The Details Dim intX As Integer intX = MsgBox("Do this now", _ vbCritical, "Prompting Issue")

  35. The value of “intZ” above depends on which response is chosen. “Yes” - intZ = 6 “No” - intZ = 7 (the numerical values are defined by Visual Basic.) Response Choices Dim intZ As Integer intZ = MsgBox(“Should I Do This”, _ vbYesNo ,”Prompting Issue”)

  36. Icon Choices

More Related