Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.

Similar presentations


Presentation on theme: "CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009."— Presentation transcript:

1 CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009

2 2 Importance of Variables Everything in computers is data-related Variables hold data Serve as a repository for a data value Serve as a repository for a data value Save purpose as variables in math Save purpose as variables in math Variable is not permanent storage Lives only for particular life span Lives only for particular life span Called “scope” Called “scope” We'll discuss scope later in the semester We'll discuss scope later in the semester

3 3 Importance of Data Types Defines the type of data contained by that variable Note this differs from variables in math, which can hold any value Note this differs from variables in math, which can hold any value Allows for easy formatting Data values of some data types cannot be read by other data types Leads to data conversion functions Leads to data conversion functions We'll discuss data conversion functions later in the semester We'll discuss data conversion functions later in the semester

4 4 Categories of Data Types Discrete Holds an individual data value Holds an individual data valueVariant Could hold a value of different types Could hold a value of different typesEnumerated Holds one of a specific list of values Holds one of a specific list of values Not discussed in this course Not discussed in this courseComplex Holds multiple values Holds multiple values

5 5 Discrete Data Types Boolean Contains only two values Contains only two values Used for True and False Used for True and False Numeric data types Integral or whole numbers Integral or whole numbers Decimal numbers Decimal numbersText

6 6 Boolean data type Holds only True or False Note that True and False are reserved words in Visual Basic. They can't be used for ANYTHING other the value of Boolean variables Note that True and False are reserved words in Visual Basic. They can't be used for ANYTHING other the value of Boolean variables Used to hold results of comparisons For example, A < B would result in True or False For example, A < B would result in True or False Can be set by program: boolVar = True boolVar = True Value can be tested in decision statements

7 7 Integral Data Types Byte Holds values 0-255 Holds values 0-255 Fastest calculation Fastest calculationShort Holds -32,767 to 32,767 Holds -32,767 to 32,767Integer Holds positive or negative value up to over 2 billion Holds positive or negative value up to over 2 billionLong Holds extremely large positive or negative values Holds extremely large positive or negative values

8 8 Decimal Data Types Single Holds positive and negative values with 7 digits and magnitude to 1.2 x 10 38 Holds positive and negative values with 7 digits and magnitude to 1.2 x 10 38Double Holds huge values with 15 digits Holds huge values with 15 digitsDecimal More decimal significance (29 digits) More decimal significance (29 digits) Magnitude to 7.9 x 10 28 Magnitude to 7.9 x 10 28

9 9 String type Used for text Dynamic – adds space as text string grows Limit of over 250,000 characters A character is any symbol on the keyboard A character is any symbol on the keyboard Includes punctuation marks and space Includes punctuation marks and space Can hold a single character Improvement over Char type, which holds only one character Improvement over Char type, which holds only one character

10 10 Handy Reference The FAQ file on the website lists these There is also a list of Visual Basic data types on the inside front cover of your text book Upper left corner Upper left corner

11 11 Numeric Calculations If two variables are contained in calculation, what happens? Any numeric types retain value Any numeric types retain value Integral to decimal simply add decimal places Integral to decimal simply add decimal places Decimal to integral rounds off decimal Decimal to integral rounds off decimal Text to numeric (or vice versa) requires conversion function Text to numeric (or vice versa) requires conversion function

12 12 Declaring a variable All variables must be declared before used They don’t need initial values Most compilers initialize them when created Most compilers initialize them when created Good practice Good practice Visual Basic command: DIM varname AS type Example: Example: DIM Count AS Integer

13 13 Declaring examples DIM Count AS Byte DIM Sum AS Integer DIM Sqr_root As Single DIM Salary AS Decimal Dim Name As String Note I switched from all capital letters to mixed case Visual Basic compiler won't matter Visual Basic compiler won't matter In class, I'll usually use all capital letters to denote reserved keywords in Visual Basic In class, I'll usually use all capital letters to denote reserved keywords in Visual Basic

14 14 Variable Names Variable names can contain letters and numbers or underscores (_) Must start with a letter Cannot contain spaces Spaces are used as keyword separators in Visual Basic Spaces are used as keyword separators in Visual Basic That's why the underscore can be used That's why the underscore can be used

15 15 Naming Conventions Your text recommends starting your variables with a three-letter designation Designation states the data type Designation states the data type Not necessary, but it's a good practice Not necessary, but it's a good practice

16 16 Variable type prefixes BooleanByteShortIntegerLongSingleDoubleDecimalStringDateBlnBytSrtIntLngSngDblDecStrdat

17 17 Variant Sometimes you want to use a variable as temporary placeholder Could hold different values at different times Could hold different values at different times Declare as Variant data type DIM temp As Variant Variants must be converted for any calculation or processing Undeclared variables are automatically made Variant

18 18 Complex Data Types Date/TimeUser-defined Can contain different data types Can contain different data types “Wrapped” within a new data type name “Wrapped” within a new data type nameArray Multiple values of same data type Multiple values of same data type Have same variable name, but includes an index number Have same variable name, but includes an index number

19 19 Date/Time Holds both date and time Initialized to current date and time Type is DATE

20 20 User-Defined Data Types Create custom data type for application Consolidates discrete items for easy reference Allows user to declare variables of structure Individual items referenced using dot notation Uses STRUCTURE statement

21 21 User-Defined Example Student record: Structure Student Dim ID As Long Dim Name As String Dim ClassYear As Integer Dim GPA As Single End Structure

22 22 Using Student Type Declare: DIM UAlb_Student As Student Referencing: UAlb_Student.ClassYear = 2010

23 23 Arrays Easily maintain set of same items Example: array of currency for expenses Example: array of currency for expenses Need to declare how many items are contained in array Need to declare how many items are contained in arrayDeclaring: DIM expenses(12) As Single Referencing: Expenses(2) = 12.87 Number in parentheses is called index

24 24 Array of students Dim CSI101(200) as Student : CSI101(1).ID = …

25 25 2-Dimensional Arrays Used for tables Declare two limits Example: 12x12 multiplication table: Dim MultTable(12,12) As Byte


Download ppt "CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009."

Similar presentations


Ads by Google