CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic...
-
date post
30-Jan-2016 -
Category
Documents
-
view
217 -
download
0
Transcript of CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic...
![Page 1: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/1.jpg)
CSI 101CSI 101Elements of ComputingElements of Computing
Spring 2009Spring 2009Lecture #6: Data Types and Lecture #6: Data Types and
Variables in Visual BasicVariables in Visual Basic
Wednesday, Februrary 11th, 2009Wednesday, Februrary 11th, 2009
![Page 2: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/2.jpg)
2
Importance of VariablesImportance of Variables
Everything in computers is data-relatedEverything in computers is data-related
Variables hold dataVariables hold data Serve as a repository for a data valueServe as a repository for a data value Save purpose as variables in mathSave purpose as variables in math
Variable is not permanent storageVariable is not permanent storage Lives only for particular life spanLives only for particular life span Called “scope”Called “scope” We'll discuss scope later in the semesterWe'll discuss scope later in the semester
![Page 3: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/3.jpg)
3
Importance of Data TypesImportance of Data TypesDefines the type of data contained by that Defines the type of data contained by that variablevariable Note this differs from variables in math, which Note this differs from variables in math, which
can hold any valuecan hold any value
Allows for easy formattingAllows for easy formatting
Data values of some data types cannot be Data values of some data types cannot be read by other data typesread by other data types Leads to data conversion functionsLeads to data conversion functions We'll discuss data conversion functions later We'll discuss data conversion functions later
in the semesterin the semester
![Page 4: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/4.jpg)
4
Categories of Data TypesCategories of Data Types
DiscreteDiscrete Holds an individual data valueHolds an individual data value
VariantVariant Could hold a value of different typesCould hold a value of different types
EnumeratedEnumerated Holds one of a specific list of valuesHolds one of a specific list of values Not discussed in this courseNot discussed in this course
ComplexComplex Holds multiple valuesHolds multiple values
![Page 5: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/5.jpg)
5
Discrete Data TypesDiscrete Data Types
BooleanBoolean Contains only two values Contains only two values Used for True and FalseUsed for True and False
Numeric data typesNumeric data types Integral or whole numbersIntegral or whole numbers Decimal numbersDecimal numbers
TextText
![Page 6: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/6.jpg)
6
Boolean data typeBoolean data typeHolds only True or FalseHolds only True or False Note that True and False are reserved words in Note that True and False are reserved words in
Visual Basic. They can't be used for ANYTHING Visual Basic. They can't be used for ANYTHING other the value of Boolean variablesother the value of Boolean variables
Used to hold results of comparisonsUsed to hold results of comparisons For example, A < B would result in True or FalseFor example, A < B would result in True or False
Can be set by program:Can be set by program: boolVar = TrueboolVar = True
Value can be tested in decision statementsValue can be tested in decision statements
![Page 7: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/7.jpg)
7
Integral Data TypesIntegral Data Types
ByteByte Holds values 0-255Holds values 0-255 Fastest calculationFastest calculation
ShortShort Holds -32,767 to 32,767Holds -32,767 to 32,767
IntegerInteger Holds positive or negative value up to over 2 Holds positive or negative value up to over 2
billionbillion
LongLong Holds extremely large positive or negative Holds extremely large positive or negative
valuesvalues
![Page 8: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/8.jpg)
8
Decimal Data TypesDecimal Data Types
SingleSingle Holds positive and negative values with 7 Holds positive and negative values with 7
digits and magnitude to 1.2 x 10digits and magnitude to 1.2 x 103838
DoubleDouble Holds huge values with 15 digitsHolds huge values with 15 digits
DecimalDecimal More decimal significance (29 digits)More decimal significance (29 digits) Magnitude to 7.9 x 10Magnitude to 7.9 x 102828
![Page 9: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/9.jpg)
9
String typeString type
Used for textUsed for text
Dynamic – adds space as text string growsDynamic – adds space as text string grows
Limit of over 250,000 charactersLimit of over 250,000 characters A character is any symbol on the keyboardA character is any symbol on the keyboard Includes punctuation marks and spaceIncludes punctuation marks and space
Can hold a single characterCan hold a single character Improvement over Char type, which holds only Improvement over Char type, which holds only
one characterone character
![Page 10: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/10.jpg)
10
Handy ReferenceHandy Reference
The FAQ file on the website lists theseThe FAQ file on the website lists these
There is also a list of Visual Basic data There is also a list of Visual Basic data types on the inside front cover of your text types on the inside front cover of your text bookbook Upper left cornerUpper left corner
![Page 11: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/11.jpg)
11
Numeric CalculationsNumeric Calculations
If two variables are contained in If two variables are contained in calculation, what happens?calculation, what happens? Any numeric types retain valueAny numeric types retain value Integral to decimal simply add decimal placesIntegral to decimal simply add decimal places Decimal to integral rounds off decimalDecimal to integral rounds off decimal Text to numeric (or vice versa) requires Text to numeric (or vice versa) requires
conversion functionconversion function
![Page 12: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/12.jpg)
12
Declaring a variableDeclaring a variable
All variables must be declared before usedAll variables must be declared before used
They don’t need initial valuesThey don’t need initial values Most compilers initialize them when createdMost compilers initialize them when created Good practiceGood practice
Visual Basic command:Visual Basic command:DIM varname AS typeDIM varname AS type Example:Example:
DIM Count AS IntegerDIM Count AS Integer
![Page 13: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/13.jpg)
13
Declaring examplesDeclaring examplesDIM Count AS ByteDIM Count AS ByteDIM Sum AS IntegerDIM Sum AS IntegerDIM Sqr_root As SingleDIM Sqr_root As SingleDIM Salary AS DecimalDIM Salary AS DecimalDim Name As StringDim Name As String
Note I switched from all capital letters to Note I switched from all capital letters to mixed casemixed case Visual Basic compiler won't matterVisual Basic compiler won't matter In class, I'll usually use all capital letters to In class, I'll usually use all capital letters to
denote reserved keywords in Visual Basicdenote reserved keywords in Visual Basic
![Page 14: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/14.jpg)
14
Variable NamesVariable Names
Variable names can contain letters and Variable names can contain letters and numbers or underscores (_)numbers or underscores (_)
Must start with a letterMust start with a letter
Cannot contain spacesCannot contain spaces Spaces are used as keyword separators in Spaces are used as keyword separators in
Visual BasicVisual Basic That's why the underscore can be usedThat's why the underscore can be used
![Page 15: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/15.jpg)
15
Naming ConventionsNaming Conventions
Your text recommends starting your Your text recommends starting your variables with a three-letter designationvariables with a three-letter designation Designation states the data typeDesignation states the data type Not necessary, but it's a good practiceNot necessary, but it's a good practice
![Page 16: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/16.jpg)
16
Variable type prefixesVariable type prefixes
BooleanBoolean
ByteByte
ShortShort
IntegerInteger
LongLong
SingleSingle
DoubleDouble
DecimalDecimal
StringString
DateDate
BlnBln
BytByt
SrtSrt
IntInt
LngLng
SngSng
DblDbl
DecDec
StrStr
datdat
![Page 17: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/17.jpg)
17
VariantVariant
Sometimes you want to use a variable as Sometimes you want to use a variable as temporary placeholdertemporary placeholder Could hold different values at different timesCould hold different values at different times
Declare as Variant data typeDeclare as Variant data typeDIM temp As VariantDIM temp As Variant
Variants must be converted for any Variants must be converted for any calculation or processingcalculation or processing
Undeclared variables are automatically Undeclared variables are automatically made Variantmade Variant
![Page 18: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/18.jpg)
18
Complex Data TypesComplex Data Types
Date/TimeDate/Time
User-definedUser-defined Can contain different data typesCan contain different data types ““Wrapped” within a new data type nameWrapped” within a new data type name
ArrayArray Multiple values of same data typeMultiple values of same data type Have same variable name, but includes an Have same variable name, but includes an
index numberindex number
![Page 19: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/19.jpg)
19
Date/TimeDate/Time
Holds both date and timeHolds both date and time
Initialized to current date and timeInitialized to current date and time
Type is DATEType is DATE
![Page 20: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/20.jpg)
20
User-Defined Data TypesUser-Defined Data Types
Create custom data type for applicationCreate custom data type for application
Consolidates discrete items for easy Consolidates discrete items for easy referencereference
Allows user to declare variables of Allows user to declare variables of structurestructure
Individual items referenced using dot Individual items referenced using dot notationnotation
Uses STRUCTURE statementUses STRUCTURE statement
![Page 21: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/21.jpg)
21
User-Defined ExampleUser-Defined Example
Student record:Student record:Structure StudentStructure Student
Dim ID As LongDim ID As Long
Dim Name As StringDim Name As String
Dim ClassYear As IntegerDim ClassYear As Integer
Dim GPA As SingleDim GPA As Single
End StructureEnd Structure
![Page 22: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/22.jpg)
22
Using Student TypeUsing Student Type
Declare:Declare:DIM UAlb_Student As StudentDIM UAlb_Student As Student
Referencing:Referencing:UAlb_Student.ClassYear = 2010UAlb_Student.ClassYear = 2010
![Page 23: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/23.jpg)
23
ArraysArrays
Easily maintain set of same itemsEasily maintain set of same items Example: array of currency for expensesExample: array of currency for expenses Need to declare how many items are Need to declare how many items are
contained in arraycontained in array
Declaring:Declaring:DIM expenses(12) As SingleDIM expenses(12) As Single
Referencing:Referencing:Expenses(2) = 12.87Expenses(2) = 12.87
Number in parentheses is called indexNumber in parentheses is called index
![Page 24: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/24.jpg)
24
Array of studentsArray of students
Dim CSI101(200) as StudentDim CSI101(200) as Student
::
CSI101(1).ID = …CSI101(1).ID = …
![Page 25: CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.](https://reader036.fdocuments.net/reader036/viewer/2022062809/56649d565503460f94a35000/html5/thumbnails/25.jpg)
25
2-Dimensional Arrays2-Dimensional Arrays
Used for tablesUsed for tablesDeclare two limitsDeclare two limitsExample: 12x12 multiplication table:Example: 12x12 multiplication table:Dim MultTable(12,12) As ByteDim MultTable(12,12) As Byte