1 / 20

Topics

Topics. Intro to Variables Variable Value Types Variable Scope Static, In-line Assignment, Option Explicit Constants Variable Naming Value Conversions.

havard
Télécharger la présentation

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 • 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

  2. 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)

  3. 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

  4. 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

  5. Built In Value Types VB KeywordMemory Size in BytesGeneric .Net data typeSize of Contents

  6. Built In Value Types (cont.)

  7. 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

  8. 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

  9. 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

  10. 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

  11. Variable Scope (cont.) • Scope Demonstration

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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.

  20. 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

More Related