PF101_Week-5_Data-Handling_V2.pptx
Document Details
Uploaded by DiversifiedCliff
Related
- PCSII Depression/Anxiety/Strong Emotions 2024 Document
- A Concise History of the World: A New World of Connections (1500-1800)
- Human Bio Test PDF
- University of Santo Tomas Pre-Laboratory Discussion of LA No. 1 PDF
- Vertebrate Pest Management PDF
- Lg 5 International Environmental Laws, Treaties, Protocols, and Conventions
Full Transcript
WEEK 5 DATA HANDLING PF101 - OBJECT ORIENTED PROGRAMMING LESSON 5 – Data Handling At the end of the session, the students should be able 1. Differentiate Types of Variables 2. Explain different uses of Variables 3. D...
WEEK 5 DATA HANDLING PF101 - OBJECT ORIENTED PROGRAMMING LESSON 5 – Data Handling At the end of the session, the students should be able 1. Differentiate Types of Variables 2. Explain different uses of Variables 3. Define Constant, Modifiers, Statement and Directiv 4. Enumerate Visual Basic Operators and Data types PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 2 WEEK 5 – Data Handling Handling Data - is managing and work with your data. Variables - Provide a means to store data values that are not stored in files. - Support the computation of values through their use in formulas in assignment statements. - Represent locations in a computer's memory. - Are assigned a unique name for reference when writing computer code - Data can be stored using variables. - Variables are used for storing values in your program. - Variables - global (public anywhere) can called in different forms and local (private) can called only within the form - Data type is a scheme of using bits to represent values (arbitrary bits) PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 3 WEEK 5 – Data Handling This figure illustrates the concept of a variable name associated with locations in random access memory. Values are stored to memory locations (represented by the rectangle). The stored values can vary over time. Each memory location is assigned a unique variable name that is used to reference the location when you write a program. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 4 WEEK 5 – Data Handling Types of Data Visual Basic provides numerous data types to allow programmers to optimize the use of computer memory. Each data type is described in the table shown here. Note the following: Data type name Description of data stored Memory storage requirement in bytes PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 5 WEEK 5 – Data Handling Unicode: A universal character encoding standard that includes a wide variety of characters, symbols, and emojis. Floating-point numbers - are numbers that can have fractional parts Default values of each data types Precision: 1. Double: A 64-bit floating-point data type with high precision (15-16 decimal digits) and a wide range. 2. Single: A 32-bit floating-point data type with lower precision (6-7 decimal digits) and a smaller range. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 6 WEEK 5 – Data Handling Binary Data: Data represented in binary form (0s and 1s) ASCII (American Standard Code for Information Interchange) - is a character encoding standard that represents text using numerical values * PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 7 WEEK 5 – Data Handling Most business applications primarily use String, Decimal, Single, and Integer data types. Here are examples of data that may be stored to computer memory: Customer Name – String – Stores alphabetic characters. Social Security Number – String – Numbers that are not used in computations. Customer Number – String – Numbers that are not used in computations. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 8 WEEK 5 – Data Handling Balance Due – Decimal – Used in calculations and often requires a decimal point to separate dollars and cents. Quantity in Inventory – Integer or Short – Selection depends on how large the quantity in inventory will be, but the quantity is usually a whole number. Sales Tax Rate – Single or Decimal – Used to store a percentage value; also used for scientific calculations. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 9 WEEK 5 – Data Handling Naming Rules for Variables and Constants You as the programmer decide what to name variables and constants – there are technical rules you must follow when creating variable and constant names. Names can include: Letters Digits Underscore But must begin with a letter Names cannot contain spaces or periods. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 10 WEEK 5 – Data Handling Names cannot be Visual Basic reserved words such as LET, PRINT, DIM, or CONST. Names are not case sensitive. This means that TotalInteger, TOTALINTEGER, and totalinteger are all equivalent names. For all intensive purposes, a Name can be as long as you want (the actual limit is 16,383 characters in length). PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 11 WEEK 5 – Data Handling Declaring Variables Declare variables that are local with the Dim statement. Declare variables that are module-level with the Private statement. Dim statement – use this to declare variables and constants inside a procedure these are local variables. "Dimension," and its primary role is to define variables, arrays, and objects, as well as to set their data types and initial values. The Dim statement needs to specify a variable/constant name and data type. Specifying an initial value for a variable is optional. Constants must have an initial value specified. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 12 WEEK 5 – Data Handling Examples: Dim StudentNameString As String Dim CountStudentsInteger As Integer Dim AccountBalanceDecimal As Decimal = 100D You can also declare more than one variable in a Dim statement. Dim StudentNameString, MajorString As String Dim SubtotalDecimal, TotalDecimal, TaxAmountDueDecimal As Decimal PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 13 WEEK 5 – Data Handling Declaring Constants Declare constants with the Const statement. Constants are similar to variables – constants are values that are stored to memory locations; however, a constant cannot have its value change during program execution – constant values are generally fixed over time. Examples: VAT Estate tax rate Name of one of the states in the United States Normal blood pressure Body temperature PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 14 WEEK 5 – Data Handling Visual Basic has two different types of constants. Intrinsic Constants – these are defined as enumerations such as Color.Red and Color.Blue. These are called intrinsic because they are predefined in Visual Basic and always exist for your use. Ex. PI = 3.14159 Named Constants – these are constants you define with a Const statement. These constants are specific to your programming application. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 15 WEEK 5 – Data Handling Examples: Const SALES_TAX_RATE_SINGLE As Single = 0.0725F Const BIG_STATE_NAME_STRING As String = "Alaska" Const TITLE_STRING As String = "Data Entry Error" Const MAX_SIZE_INTEGER As Integer = 4000 String (text or character) constants are assigned values within the " " (double quote) marks. This statement stores double- quote marks as part of the string – do this by typing two double-quote marks together. Const COURSE_TITLE_STRING As String = ""Programming Visual Basic"" PF101-OBJECT ORIENTED PROGRAMMING PF101 – OBJECT ORIENTED PROGRAMMING 16 WEEK 5 – Data Handling Const MaxItems As Integer = +100 Numeric constants like those shown above do NOT use double-quote marks – just type the numeric value numbers. Follow these rules for assigning numeric values to constants: You can use numbers, a decimal point, and a plus (+) or minus (-) sign. Do not include special characters such as a comma, dollar sign, or other special characters. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 17 WEEK 5 – Data Handling Append one of these characters to the end of the numeric constant or variable to denote a data type declaration. If you do not use these, a whole number is assumed to be Integer and a fractional value is assumed to be Double. Decimal D 40.45D Double R 12576.877R Integer I 47852I Long L 9888444222L Short S 2588S Single F 0.0725F PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 18 WEEK 5 – Data Handling Scope of Variables and Constants Each variable (or constant) has a finite lifetime and visibility – termed the Scope. The variable lifetime is how long the variable exists before the computer operating system garbage-collects the memory allocated to the stored value. * There are four levels of scope: Namespace Module level Local Block PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 19 WEEK 5 – Data Handling Scope of Variables and Constants 1. Namespace (use a Public Shared declaration instead of Dim) The variable is visible within the entire project (applicable to a project with multiple forms). The variable is project-wide and can be used in any procedure in any form in the project. The variable memory allocation is garbage-collected when application execution terminates. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 20 WEEK 5 – Data Handling 2. Module level (usually use Private to declare the variable; use Const to declare the constant) A variable/constant can be used in any procedure on a specific Form – it is not visible to other Forms. Declaration: Module-level variables are declared within a module but outside of any procedure or function. They are accessible by all procedures within the module. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 21 WEEK 5 – Data Handling Memory Allocation: Initialization: Module-level variables are allocating memory when the module is first accessed or initialized. This typically happens when the application starts, and the module is loaded into memory. Storage: These variables are stored in the module's data section in memory. Their lifetime extends for the duration of the application's run time, or until the module is unloaded or the application terminates. Default Value: Module-level variables are automatically initialized to their default values if not explicitly set. For instance, an Integer variable is initialized to 0. Scope and Lifetime: The scope of a module-level variable is restricted to the module in which it is declared, but it persists for the lifetime of the application. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 22 WEEK 5 – Data Handling PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 23 WEEK 5 – Data Handling 3. Local (use Dim to declare the variable; use Const to declare the constant) A variable/constant is declared and used only within a single procedure. Variable lifetime is the period for which a variable exists. Execution: When a procedure is invoked (e.g., when you click a button), a new execution context (or stack frame) is created for that procedure. This context includes space for local variables and constants. End of Execution: When the procedure finishes execution and encounters the End Sub statement (or equivalent), the stack frame for that procedure is PF101 PF101-OBJECT removed. ORIENTED PROGRAMMING – OBJECT ORIENTED PROGRAMMING 24 WEEK 5 – Data Handling 3. Local (use Dim to declare the variable; use Const to declare the constant) A variable/constant is declared and used only within a single procedure. Variable lifetime is the period for which a variable exists. New Execution: New Procedure Call: Each time the procedure is called (e.g., every button click), a new stack frame is created, and new instances of local variables and constants are initialized. This means that: Separate Instances: Each invocation of the procedure starts with a fresh set of local variables and constants. Previous values are not retained or accessible from the new call. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 25 WEEK 5 – Data Handling 4. Block (use Dim to declare the variable; use Const to declare the constant) The variable/constant is only visible within a small portion of a procedure – these variables/constants are rarely created. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 26 WEEK 5 – Data Handling This figure illustrates where to declare local versus module-level variables/constants. Later you will learn to declare namespace and block variables and constants and when to use the Public keyword in place of Private. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 27 WEEK 5 – Data Handling PF101-OBJECT ORIENTED IT11 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 28 WEEK 5 – Data Handling Converting Output Data Types In order to display numeric variable values as output data, the values must be converted from numeric data types to string in order to store the data to the Text property of a TextBox control. 1. Use the ToString method. These examples show converting strings to a numeric representation with 2 digits to the right of the decimal (N2) and currency with 2 digits to the right of the decimal (C2) as well as no digits to the right of a number (N0 – that is N zero, not N Oh). SubtotalTextBox.Text = SubtotalDecimal.ToString("N2") SalesTaxTextBox.Text = SalesTaxDecimal.ToString("C2") QuantityTextBox.Text = QuantityInteger.ToString("N0") To format a decimal number as a string with a specific number of decimal places and then assign that formatted string to a TextBox control. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 31 WEEK 5 – Data Handling Implicit Conversion This is a conversion by Visual Basic from a narrower to wider data type (less memory to more memory). Simplifies Code. Supports Operator Overloading. This is done automatically as there is no danger of losing any precision. In this example, an integer (4 bytes) is converted to a double (8 bytes): BiggerNumberDouble = SmallerNumberInteger PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 32 WEEK 5 – Data Handling Explicit Conversion This is also called Casting and is used to convert between numeric data types that do not support implicit conversion. Allows for Method Overloading and Type Flexibility. This table shows use of the Convert method to convert one numeric data type to another numeric data type. Note that fractional values are rounded when converting to integer. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 33 WEEK 5 – Data Handling 2. Use the Parse method to convert a string to a number Performing Calculations. Visual Basic uses a wider data type when calculations include unlike data types or to parse the value in a textbox control. This example produces a decimal result: AverageSaleDecimal = TotalSalesDecimal / CountInteger Summary Rules: Use the Convert method to convert a value from one data type to another. PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 34 WEEK 5 – Data Handling Arithmetic Operators The arithmetic operators are the same as in many other programming languages. They are: + Addition - Subtraction * Multiplication / Division ^ Exponentiation \ Integer Division Mod Modulus Division PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 35 WEEK 5 – Data Handling Exponentiation – This raises a number to the specified power. The result produced is data type Double. Example: ValueSquaredDouble = NumberDecimal ^ 2 ValueCubedDouble = NumberDecimal ^ 3 PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 36 WEEK 5 – Data Handling Integer Division – Divide one integer by another leaving an integer result and discarding the remainder, if any. Example: If the variable MinutesInteger= 130, then this expression returns the value of 2 hours. HoursInteger = MinutesInteger \ 60 PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 37 WEEK 5 – Data Handling Modulus Division – This returns the remainder of a division operation. Using the same value for MinutesInteger= 500, this expression returns the value 20 minutes and can be used to calculate the amount of overtime worked for an 8-hour work day. MinutesInteger = MinutesInteger Mod 60 PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 38 WEEK 5 – Data Handling Order of Precedence The order of precedence for expressions that have more than one operation is the same as for other programming languages. Evaluate values and calculation symbols in this order: (1) Values enclosed inside parentheses (2) Exponentiation (3) Multiplication and Division (4) Integer Division (5) Modulus Division (6) Addition and Subtraction PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 39 WEEK 5 – Data Handling Assignment Operators and Formulas The equal sign is the assignment operator. It means to store the value of the expression on the right side of the equal sign to the memory variable named on the left side of the equal sign. Examples: ItemValueDecimal = QuantityInteger * PriceDecimal HoursWorkedSingle = MinutesWorkedSingle / 60F NetProfitDecimal = GrossSalesDecimal – CostGoodsSoldDecimal PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 40 WEEK 5 – Data Handling The plus symbol combined with the equal sign allows you to accumulate a value in a memory variable. Examples: TotalSalesDecimal += SaleAmountDecimal Is equivalent to the following – it means take the current value of TotalSalesDecimaland add to it the value of SaleAmountDecimal and store it back to TotalSalesDecimal(it gets bigger and BIGGER). TotalSalesDecimal = TotalSalesDecimal + SaleAmountDecimal PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 41 WEEK 5 – Data Handling The minus symbol combined with the equal sign allows you to decrement or count backwards. Examples: CountInteger -= 1 is equivalent to CountInteger = CountInteger - 1 PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 42 LESSON 5 – Data Handling PF101-OBJECT ORIENTED PF101 PROGRAMMING – OBJECT ORIENTED PROGRAMMING 43