200 likes | 314 Views
Topics. Intro to Variables Variable Value Types Variable Scope Static, In-line Assignment, Option Explicit Constants Variable Naming Value Conversions.
E N D
Topics • Intro to Variables • Variable Value Types • Variable Scope • Static, In-line Assignment, Option Explicit • Constants • Variable Naming • Value Conversions “The ultimate metric for user-friendliness is quite simple: if this system was a person how long would it take before you punched it in the nose?” Tom Carey
Computer Memory • Anything currently in use by a computer program is held in the computer’s memory • Operating system • Program instructions (code) • Form & control definitions • Data and values • Variables and Constants are named locations in memory that contain values available to the program • Variables may be changed (they vary) • Constants cannot be changed (they are constant)
Variables • Declaring a variable does two things: • Reserves an area of memory of an appropriate size (later) • Creates a name with which to access the memory location (and its contents) • Use the Dim statement to declare a variable General Form Dim variablename As datatype Examples Dim intQuantity as Integer Dim stLastName as String
Variables & Size • Numeric and the Char variable types all require fixed memory sizes and are easy for VB to manage • String and Object (discussed later in the course) variable types actually refer to objects created in memory and often require dynamic memory allocation • Fortunately, we don’t have to deal with this directly • This is a performance cost of programming • But we need to work with strings in business
Built In Value Types VB KeywordMemory Size in BytesGeneric .Net data typeSize of Contents
Variables & Size (cont.) • Using appropriate variable sizes reduces the memory needed by the computer • Very important in server applications, especially web server applications • Similar logic applies when designating database field data types (ISM 4212) • Integer data types hold integers more efficiently than floating point decimal data types • Sometimes values must be converted from one type to another
Variable Scope • Variables (usually) only exist while the code in which they are declared is active • Afterwards the variable (and its memory) are released • Variables declared in one procedure are not available in another • The Scope at which a variable is declared is an important design decision
Variable Scope (cont.) • Variables declared in the Declarations Section of a form’s code (or class or module) are available to any code running in that procedure (next slide) • These variables persist as long as the form is loaded • Module-level variables lose their values when the form is closed • Variables declared within a procedure (Sub or Function) are local to just that procedure
Variable Scope (cont.) Module Scoped Public Class VariableDemoForm Dim intFirst As Integer Dim intSecond As Integer Private Sub btnAddLocal_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnAddLocal.Click Dim intOne As Integer Dim intTwo As Integer intOne = Convert.ToInt32(txtNumberOne.Text) intTwo = Convert.ToInt32(txtNumberTwo.Text) lblResults.Text = (intOne + intTwo).ToString End Sub End Class Procedure Scoped
Variable Scope (cont.) • Scope Demonstration
Variable Scope (cont.) • Variables may be declared using the Private or Public keywords instead of Dim at the module level only • May not be used for procedure-level declarations • Important in multi-form applications • You may declare a procedure-level variable with the same name as a module-level variable • The procedure will not “see” the module-level variable—it will be “masked” by the locally declared variable
The Static Keyword • “Static” may be used in place of “Dim” in procedure-level declarations • In this event, the variable’s contents do not disappear when the procedure exits • The last value assigned will be present when the procedure is called the next time • If the form containing the procedure is closed then the variable’s value is lost
Variations • Variables may be assigned a value in the same line where they are declared • More than one variable of the same type may be declared in the same statement • But I don’t like this!!! Dim sglTaxRate as Single = 0.065 Dim strLName as String = txtLName.Text Dim intQty, intQtyPerCase as Integer
Option Explicit • VB will allow you to use a variable without declaring it • This is very bad practice and is incompatible with other languages that can be used in the .Net framework • Enter Option Explicit On at the top of each module (above the Public Class statement) to force variables to be declared '* Require that all variables be declared Option Explicit On Public Class VariableDemoForm
Constants • Constants may be declared at the module or local level using the Const key word • Constants are assigned a value when they are declared • Constants may not be changed once declared (they do not vary and are not variables) • Useful for one-source declarations of unchanging values • Useful for naming a difficult to remember value Const Pi as Double = 3.14159265
Variable Naming • Name variables similarly to controls to clarify your code • Prefix to indicate variable data type • Name body to indicate contents of variable • Examples • sglPrice Single precision • intQuantity Integer • blnIsActive Boolean • lngClassLength Long integer • strLastName String
Value Conversion • The data type of a value must often be converted from its current form to the form of the variable or property into which it will be placed • VB will TRY to convert but you should be explicit • Most common conversion revolves around Text properties of controls containing numeric values • Text property contains characters or string values, even if they are numbers • To be safe you should convert these before storing them into numeric variables or properties
Value Conversion • Use the Convert statement • Typing convert and dot will present all of the choices • Note that the To-datatype lists the .Net data types, not the VB data types • See Figure 4-1 (page 101) • Other older conversions available intOne = Convert.ToInt32(txtNumberOne.Text) intOne = Val(txtNumberOne.Text) intOne = CDbl(txtNumberOne.Text) … etc.
Value Conversion (cont.) • The ToString method can be applied to any value to create a string (character) version of the value • Text properties can easily take numeric values but you can explicitly convert if you want to • Numeric variables and properties cannot contain formatting characters such as $, %, or commas • We will cover formatting techniques later to display numeric values with specific formatting lblTotalCost.Text = sglTotalCost.ToString