1 / 9

Topics

Topics. Data Types for variables Constants to replace literals Creating variables Variable scoping Naming variables User-defined type example. Integer Integer Long Floating point Single Double Currency String Variant User Defined Multi-element variable. -32,768 – 32,767

kasia
Download Presentation

Topics

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. Topics • Data Types for variables • Constants to replace literals • Creating variables • Variable scoping • Naming variables • User-defined type example Jeffrey P. Landry, University of South Alabama

  2. Integer Integer Long Floating point Single Double Currency String Variant User Defined Multi-element variable -32,768 – 32,767 -2.147 billion – 2.147 billion 1.4012e-45 –3.4028e38 4.94065e-324 – 1.79769e308 922,337 trillion Up to 65,500 characters Any value above plus dates Composed of collections of individual data elements Data Types Jeffrey P. Landry, University of South Alabama

  3. Used in place of literal values Declare in the general section of modules Good programming practice to use constants literals tell the programmer nothing changing in more than one place Public Const RptHead$ = “Payroll Report” Const MinRate As Currency = 5.50 Const MaxRate As Currency = 75.50 ‘* Get employee rate from user If (EmpRate < MinRate) Or (EmpRate > MaxRate) Then MsgBox “Invalid pay rate”,... Constants Jeffrey P. Landry, University of South Alabama

  4. Variables • VB supports several types of variables • may be implicitly or explicitly declared • Three aspects of variables to focus on • Data type • Scope • Name • Keywords • Object names • Scope overlap Jeffrey P. Landry, University of South Alabama

  5. Creating Variables • Introduce variable (bad!) • Let Num = 23 • Declare, or dimension variables (good!) • with Dim, Public, Private, Static • Dim RecNum As Integer • Dim RecNum% • Option Explicit statement (very good!) • Placed in declarations section of form or module • Forces declaration (dimensioning) of all variables before use • Causes compile error if violated Jeffrey P. Landry, University of South Alabama

  6. Variable Scoping • Three levels • local - Variables first used or declared in a procedure are local to that procedure • module-level - Variables declared with Dim or Private keyword in the General section of a form or code module are available to any procedure in that module • global - Variables declared with Public keyword in General section are accessible throughout the application Public FileName As String ‘* Database file name Jeffrey P. Landry, University of South Alabama

  7. Variable Scoping (cont.) • Declaring a variable at a local (procedure) level will “mask” a broader variable of the same name • Failure to declare local variables might produce unintended consequences • such as changing the value of a global or module-level variable • Local variables lose value when procedure terminates • unless declared as static with Static statement • Module and global variables always retain value throughout program Jeffrey P. Landry, University of South Alabama

  8. Variable Naming For I% = 1 to Count%M Next I% • Same rules as for naming objects • up to 40 characters, beginning with letter • letters, numbers, underscore • no keywords • Competition for names with objects and keywords • Naming conventions • I, J, K, X, Y, Z counters • Editor auto-adjusts upper/lower case Jeffrey P. Landry, University of South Alabama

  9. Type ProductType Name As String * 20 Price As Currency Cost As Currency OnHand As Integer End Type Dim Table As ProductType Let Table.Name = “Tables” Let Table.Price = 149.50 Let Table.Cost = 95.00 Let Table.OnHand = 75 User-Defined Type Example Jeffrey P. Landry, University of South Alabama

More Related