Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 31 Variable Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data.

Similar presentations


Presentation on theme: "Tutorial 31 Variable Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data."— Presentation transcript:

1 Tutorial 31 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

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

3 Tutorial 33 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

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

5 Tutorial 35 Three-character Ids Byte byt Booleanbln Currency cur Date/Time dtm Doubledbl Integerint Longlng Objectobj Singlesng Stringstr Variantvnt

6 Tutorial 36 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

7 Tutorial 37 Creating (declaring) a Variable Dim variablename [As datatype] Public variablename [As datatype]

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

9 Tutorial 39 Constants 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

10 Tutorial 310 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

11 Tutorial 311 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

12 Tutorial 312 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

13 Tutorial 313 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

14 Tutorial 314 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

15 Tutorial 315 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 = Public Const conMaxAge as Integer = 65

16 Tutorial 316 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

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

18 Tutorial 318 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

19 Tutorial 319 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

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

21 Tutorial 321 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

22 Tutorial 322 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

23 Tutorial 323 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, Properties, Startup Object list to specify the startup form

24 Tutorial 324 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

25 Tutorial 325 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

26 Tutorial 326 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

27 Tutorial 327 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

28 Tutorial 328 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

29 Tutorial 329 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.

30 Tutorial 330 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

31 Tutorial 331 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

32 Tutorial 332 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

33 Tutorial 333 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


Download ppt "Tutorial 31 Variable Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data."

Similar presentations


Ads by Google