90 likes | 259 Views
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
E N D
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
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
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
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
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
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
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
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
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