variable n.
Skip this Video
Loading SlideShow in 5 Seconds..
Variable PowerPoint Presentation


123 Vues Download Presentation
Télécharger la présentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Variable • Memory location whose value can change as the program is running. • Used to hold temporary information • Used to control the type of data used in calculations • Val returns a Double-type, which is often larger than necessary • Can store only one piece of data at any time • Data is processed faster Tutorial 3

  2. Byte Boolean Currency Date Double Integer Long Object Single String Variant Data Types Tutorial 3

  3. Use the Appropriate Data Type • Integer or Long - Used to store whole numbers • Single, Double, Currency - Used to store numbers with a decimal fraction • String - Used to store strings • Boolean - Used to store Boolean values (True and False) • Date - Used to store date and time information • Object - Used to store a reference to an object • Byte - Used to store binary data • Variant - Flexible, but not efficient Tutorial 3

  4. Variable Names • Should be meaningful • First three characters should represent the data type • Remainder of name should represent the variable’s purpose Tutorial 3

  5. Byte byt Boolean bln Currency cur Date/Time dtm Double dbl Integer int Long lng Object obj Single sng String str Variant vnt Three-character Ids Tutorial 3

  6. Rules for Naming Variables • Name must begin with a letter • Name can contain only letters, numbers, and the underscore. No punctuation characters or spaces are allowed • Name cannot exceeds 255 characters • Name cannot be a reserved word Tutorial 3

  7. Creating (declaring) a Variable • Dim variablename [As datatype] • Public variablename [As datatype] Tutorial 3

  8. Assigning Values to Variables • Assignment statement • variablename = value • Examples: • sngHours = 38.5 • curBonus = curSales * .1 • strName = “Susan” Tutorial 3

  9. Literal constant an item of data whose value cannot change while the program is running Examples: 7 “Janet” Symbolic constant a memory location whose contents cannot be changed while the program is running Examples: conPi conRate Constants Tutorial 3

  10. Scope of a Variable • Indicates which procedures can use the variable • Determined by where the Dim or Public statement is entered • Can be either global, form-level, or local Tutorial 3

  11. Local Variables • Created with the Dim statement • The Dim statement is entered in an object’s event procedure • Only the procedure in which it is declared can use the variable • Removed from memory when the procedure ends Tutorial 3

  12. Form-level Variables • Created with the Dim statement • The Dim statement is entered in a form’s General declarations section • Can be used by any of the procedures in the form • Removed from memory when the application ends Tutorial 3

  13. Global Variables • Created with the Public statement • The Public statement is entered in a code module’s General declarations section • Used in multi-form projects and can be used by any of the procedures in any of the project’s forms • Removed from memory when the application ends Tutorial 3

  14. Option Explicit Statement • Doesn’t allow you to create variables “on the fly” • Enter in every form’s, and every code module’s, General declarations section • Use Tools, Options, Environment tab, Require Variable Declaration to have Visual Basic include Option Explicit in every new form and module Tutorial 3

  15. Creating a Symbolic Constant • A memory location whose value cannot change during run time • Syntax: [Public] Const constname [As datatype] = expression • Examples: • Const conPi As Single = 3.141593 • Public Const conMaxAge as Integer = 65 Tutorial 3

  16. Scope of a Symbolic Constant • Indicates which procedures can use the symbolic constant • Global: Public Const statement in a code module’s General declarations section • Form-level: Const statement in the form’s General declarations section • Local: Const statement in an event procedure Tutorial 3

  17. String Concatenation • Ampersand - & • Examples:(Assume strFirstName contains “Mary” and sngSales contains 1000) • “Hello “ & strFirstName • strFirstName & “ sold $“ & sngSales & “.” • Results: • Hello Mary • Mary sold $1000 Tutorial 3

  18. InputBox function • Displays one of Visual Basic’s predefined dialog boxes • Contains a message, along with an OK button, a Cancel button, and an input area • Syntax: InputBox(prompt, title) • Use sentence capitalization for the prompt, and book title capitalization for the title • Has limitations: can’t control appearance and allows user to enter only one piece of data Tutorial 3

  19. Newline Character • Chr(13) & Chr(10) - issues a carriage return followed by a line feed • vbNewLine - one of Visual Basic’s intrinsic constant • An intrinsic constant is one that is built into the Visual Basic language Tutorial 3

  20. Object Browser • Dialog box that provides information about objects available to your application • The Object Browser lists properties, methods, events, and intrinsic constants Tutorial 3

  21. Default Command Button • Can be selected by pressing the Enter key even when the button does not have the focus • Set the button’s Default property to True • Only one command button can be the default • If used, it is typically the first button • If a button’s action is destructive and irreversible, then it should not be the default button Tutorial 3

  22. InputBox Function • Has the following limitations: • Can’t control its appearance • Allows the user to enter only one piece of data • Used for RAD (rapid application development] • In the final project, InputBox functions are typically replaced with professional-looking dialog boxes Tutorial 3

  23. Multi-form Projects • Only one form, called the startup form, is automatically loaded and displayed • You must include code to load/display the other forms in the project • Use the Project menu, <Project Name> Properties, Startup Object list to specify the startup form Tutorial 3

  24. Loading and Displaying a Form • Visual Basic has two statements and two methods that control the loading and displaying of forms • Load statement • Unload statement • Hide method • Show method Tutorial 3

  25. Load and Unload Statements • Load statement • brings a form into memory, but does not display the form on the screen • Syntax: Load object • Unload statement • removes a form from both memory and the screen • Syntax Unload object Tutorial 3

  26. Show and Hide Methods • Show method • displays a form on the screen; loads the form if it is not already in memory • Syntax: object.Show [style], where style , which is optional, can be either 0 or 1 • Hide method • removes a form from the screen, but leaves it in memory • Syntax: object.Hide Tutorial 3

  27. Style • 0 or omitted means that the form is modeless • Example: MSDN Library window • 1 means that the form is modal • Example: Visual Basic’s Open Project dialog box Tutorial 3

  28. Standard Windows Dialog Box • Created from a form • Centered on the screen • Not resizable • Contains only a Close button • Set the form’s BorderStyle property to 3-Fixed Dialog Tutorial 3

  29. Centering Instructions • formname.Top = (Screen.Height - formname.Height)/2 • formname.Left = (Screen.Width - formname.Width)/2 • Top, Left, Height, and Width properties are measured in twips • One twip is 1/1440 of an inch. Tutorial 3

  30. Timer Control • Processes code at regular intervals • Interval property • Measured in milliseconds • A millisecond is 1/1000 of a second • Timer event • Contains the code that will be processed when each interval has elapsed Tutorial 3

  31. Removing a Coded Control • Remove all of the control’s code before removing the control • Unassociated code remains in the application • Look in the form’s General declarations section to verify that the application does not contain any unassociated code Tutorial 3

  32. Appearance of the Mouse Pointer • Controlled by the object’s MousePointer property • Use either an hourglass or an arrow/hourglass to indicate that the application is busy • The hourglass indicates that the mouse pointer is temporarily inactive, whereas the arrow/hourglass indicates that the mouse pointer still can be used in the current application Tutorial 3

  33. Debugging Technique • Always enter the Option Explicit statement in the General declarations of every form and module • If your application uses the InputBox function, test your application to see how it handles the various InputBox responses • When using the Val function, remember that Visual Basic must be able to interpret the string expression as a numeric value Tutorial 3