cbvp2103 a storage location in memory ram
play

CBVP2103 A storage location in memory (RAM) Holds data/information - PowerPoint PPT Presentation

CBVP2103 A storage location in memory (RAM) Holds data/information while the program is running These storage locations can be referred to by their names Every variable has three properties: Name - reference to the location -


  1. CBVP2103

  2.  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can be referred to by their names  Every variable has three properties:  Name - reference to the location - cann nnot ot be c chan anged ged  Value ue - the information that is stored - can be c chan anged ged during program execution , hence the name “variable”  Data Type - the type of information that can be stored - canno nnot be c chan anged ged

  3.  A variable is something that you store a value in as you work through your algorithm. You can then make a decision based on that value (for example, “Is it equal to 7?”, “Is it more than 4?”), or you can perform operations on that value to change it into something else (for example, “Add 2 to the value”, “Multiply it by 6”, and so on).

  4. Dim intNumber As Integer intNumber = 27 intNumber = intNumber + 1 MessageBox.Show (“Value of intNumber + 1 = “ & intNumber , “Variables”)

  5.  Copy and store values entered by the user  Perform arithmetic manipulation on values  Test values to see if they meet a criteria  Temporarily hold and manipulate the value of a control property  Hold data/information so that it can be recalled for use at a later point in the code

  6. Data type - Specifies type of data variable can store  Integer variables: Long, Integer, Short, Byte  Floating-point variables: Single, Double  Fixed decimal point variable: Decimal  Boolean variables: True, False  Character variable: Char  Text variable: String  The Object variable ◦ Default data type assigned by Visual Basic ◦ Can store many different types of data ◦ Less efficient than other data types

  7. Data a type Prefix Size ze Values lues Byte byt 1 byte positive integer value from 0 to 255 Short shr 2 byte integer from – 32,768 to +32,767 Integerint 4 byte integer from +/- 2,147,483,647 Long lng 8 byte integer from +/- 9,223,372,036,854,775,807 Single sng 4 byte single-precision, floating-point number Doubledbl 8 byte double-precision, floating-point number Decimal dec 16 byte number with up to 28 significant digits Char chr 2 byte Any single character Boolean bln 2 byte True or False String str (4 byte) Text - Any number/combination of characters Date dtm 8 byte 8 character date: #dd/mm/yyyy# Object obj (4 byte) An address that refers to an object

  8. Keyword Description Special format for literals bool true false Boolean char 'A' '\x0041' '\u0041' 16 bit Unicode character sbyte 8 bit signed integer none byte 8 bit unsigned integer none short 16 bit signed integer none ushort 16 bit unsigned integer none int 32 bit signed integer none uint U suffix 32 bit unsigned integer long L or l suffix 64 bit signed integer ulong U/u and L/l suffix 64 bit unsigned integer float F or f suffix 32 bit floating point double 64 bit floating point no suffix decimal M or m suffix 128 bit high precision string "hello", @"C:\dir\file.txt" character sequence

  9.  Data types in Visual Basic fall into two broad categories: (1) value types and (2) reference types . Value types and reference types differ primarily in how they are stored in memory. The memory allocated to a value type variable contains the actual value. In a statement such as: Dim simpleValue As Integer = 5

  10.  a memory location is set aside to hold the value of 5. In contrast, the memory storage allocated to a reference type variable stores another memory address location where the real data can be found. It's like a forwarding address at the post office. In a reference type declaration such as: Dim somewhereElse As New MyCustomClass  the VB compiler creates an instance of the MyCustomClass class in memory and then sets the value of somewhereElse to the true memory address of that instance. If you are familiar with pointers in languages such as C++, this is Visual Basic's closest equivalent.  In short, value type variables contain the data, and reference type variables point to the data.

  11.  First character must be a letter or underscore  Must contain only letters, numbers, and underscores (no spaces, periods, etc.)  Can have up to 255 characters  Cannot be a VB language keyword  Naming Conventions  Should be meaningful  Follow 3 char prefix style - 1st 3 letters in lowercase to indicate the data type  After that, capitalize the first letter of each word  Example: intTestScore

  12.  A variable declaration is a statement that creates a variable in memory  Syntax: Dim VariableName ableName As DataTyp Type ◦ Dim (short for Dimension) - keyword ◦ VariableName - name used to refer to variable ◦ As - keyword ◦ DataType - one of many possible keywords to indicate the type of value the variable will contain  Example: Dim intLength ength as Integer eger

  13.  A starting or initialization value may be specified with the Dim statement  Good practice to set an initial value unless assigning a value prior to using the variable  Syntax: Dim Variabl ableNa eName As DataTyp Type = = Value e  Just append " = value” to the Dim statement  = 5  assigning a beginning value to the variable  Example: Dim intLength ength as Integer eger = 5

  14.  Variable MUST be declared prior to the code where they are used  Variable should be declared first in the procedure (style convention)  Declaring an initial value of the variable in the declaration statement is optional ◦ Refer to default values (next slide)

  15. Data a type Default ault (Initial ial) ) va value All numeric types Zero (0) Boolean False Char Binary 0 String or Object Empty Date 12:00 a.m. on January 1, 0001

  16.  Actual value/data/information  Similar to a variable, but can NOT change during the execution of a program.  Examples of Literals: ◦ Numeric: 5 ; 157 ; 195.38256 ◦ String: “Paul” ; “Hello!!!” ; “Jackson, AL 36545” ◦ Char: „a‟ ; „1‟ ; „?‟ ; „@‟ ◦ Boolean: True ; False

  17.  Programs often need to use given values ◦ For example: decTotal *= 1.06 ◦ Adds 6% sales tax to an order total  Two problems with using literals for these types of values ◦ The reason for multiplying decTotal by 1.06 isn‟t always obvious ◦ If sales tax rate changes, must find and change every occurrence of .06 or 1.06

  18.  Use of named constants resolves both these issues  Can declare a variable whose value is set at declaration and cannot be changed later:  Syntax: Cons nst t CON ONST_NAME ST_NAME As As Da DataType ype = = Va Valu lue Looks like a normal declaration except: ◦ Const used instead of Dim ◦ An initialization value is required ◦ By convention, entire name capitalized with underscore characters to separate words

  19.  The objective of our code is now clearer ◦ Const nst sngSALES SALES_TA _TAX_RA X_RATE TE As As Si Single gle = 1.06 ◦ decTot Total al *= sngSA SALES_T ES_TAX_ AX_RA RATE TE  Can change all occurrences in the code simply by changing the initial value set in the declaration ◦ If tax rate changes from 6% to 7% ◦ Const nst sngSAL SALES ES_TA _TAX_R X_RATE TE As As Si Single gle = 1. 1.07 07

  20.  What – Indicates the part of the program where the variable can be used  When – From the variable declaration until the end of the code block (procedure, method, etc.) where it is declared  Variable cannot be used before it is declared  Variable declared within a code block is only visible to statements within that code block ▪ Called Local Variable  Can be declared at the beginning of the class code window (General Declarations section) and be available to all blocks ▪ Called Form Level Variable le  Variables that share the same scope cannot have the same name (same name ok if different scope)

  21.  Syntax: va variablename blename = express ession on  Assigns the value of the expression to the variable. (The variable must be on the left and the expression on the right.)  Example: ◦ intNumber1 = 4 ◦ intNumber2 = 3 * (2 + 2) ◦ intNumber3 = intNumber1 ◦ IntNumber1 = intNumber1 + 6

  22.  A value of one data type can be assigned to a variable of a different type ◦ An implicit type conversion is an attempt to automatically convert to the receiving variable‟s data type  A widening conversion suffers no loss of data ◦ Converting an integer to a single ◦ Dim sngNumber as Single = 5  A narrowing conversion may lose data ◦ Converting a decimal to an integer ◦ Dim intCount = 12.2 „ intCount becomes 12

  23.  VB provides a set of functions that perform data type conversions  These functions will accept a literal, variable name, or arithmetic expression  The following narrowing conversions require an explicit type conversion ◦ Double to Single ◦ Single to Integer ◦ Long to Integer  Boolean, Date, Object, String, and numeric types represent different sorts of values and require conversion functions as well

  24.  The Val function is a more forgiving means of performing string to numeric conversions  Uses the form Val(string)  If the initial characters form a numeric value, the Val function will return that  Otherwise, it will return a value of zero

Recommend


More recommend