exam 3 review

2
ARRAYS AND STRUCTURES •Array : a set of related data, a list of similarly typed data, a group of related variables with the SAME data type •Element : a variable in an array •Index : unique number starting at zero assigned to each element of an array •Member : is the name of the typed variable; a variable in a structure •Populate : to assign data to the elements of an array •Structures : have DIFFERENT data types; a user-defined data type; an object that contains related variables often of mixed data types •Subscript: the element number of an array; ex: strState(39), 39 is the subscript of the array •Two-Dimensional Array : array of a single type that contains rows and columns of related data •Zero-Based : an array that numbers its elements starting at zero Concepts: ARRAYS: •An array is a series of related variables. •All variables in an array share the same name and data type. •Array variables are distinguished by their index and each item in an array has an unique index number. The unique number for each element in an array is called a subscript. •Arrays are zero-based. •All arrays must be declared before they can be used and can be declared with a specific size or no size. •Arrays can be populated when created or assigned values later •Arrays are easily manipulated with loops. •Arrays can be populated from a file or through user input. •An array can be resized at any time using ReDim. However, unless you use the Preserve keyword, the existing data will be lost when you resize the array. •A two-dimensional array works well for sets of related data. •An array of type structure can be used to work with multiple records. • Structures are similar to records in a database – they store multiple types of related data in a single container. Unlike arrays, the data types in a structure do not have to be the same. -A structure MUST be declared outside of other subs, functions and event handlers. - Use s as the prefix for a structure to make recognition easier. ARRAY SYNTAX: Dim strPaint(9) as string; array name is strPaint, array stores 10 items, ea. Item is a string, array starts empty and # in parenthesis must be a integer or a variable that represents an integer PRE-POPULATING ARRAY: Dim varName( ) As Type = { List } use curly brace; dim creates an array, varName is the name of the array, type is data type, list provides a comma separated list to populate the array; ***Leave parenthesis empty when providing a list of items for an array SINGLE ARRAY: *EXPLICITLY INITIALIZE/PUT VALUES IN ARRAY OR PUT VALUES IN A SEPARATE FUNCTON Dim decExams ( ) As Decimal { 82, 84, 92} Dim decExams(2) as Decimal •leave parentheses blank! decExams (0) = 82 decExams(1) = 84 decExams (2) = 92 TWO-DIMENSIONAL ARRAY: Dim intPoints(4, 3) As Integer; intPoints: array that stores integers; 5 rows and 4 column •Structures are similar to records in a database – they store multiple types of related data in a single container. •Unlike arrays, the data types in a structure do not have to be the same. •A structure MUST be declared outside of other subs, functions and event handlers and must be created and declared before it can be used •Use s as the prefix for a structure to make recognition easier. EXAMPLE: Dim decGrades (119, 17) As Decimal decGrades.GetUpperBound (0) gets you 119 decGrades.GetUpperBound (1) gets you 17 • zero will get you the row and the one will get you the column ***DRAWBACKS OF TWO-DIMENSIONAL ARRAYS 1. Entire array must be of the same data type 2. Reference by 2 indices (row, column); not self- documenting -fix these 2 problems with structures! STRUCTURES: 1. Create a structure OUTSIDE of event handler Public Structure sEmployee Dim strLast As String Dim strFirst As String Dim decPayRate As Decimal Dim decHours As Decimal Dim decGross As Decimal End Structure 2. Declare Structure Can create a single structure: Dim sEmployee As Employee OR Create multiple structures Dim sEmployees(9) As Employee ***10 Employees created 3.Referencing Structures & Populate Structure: Structure.Member; structure is name of the structure and member is name of the typed variable sEmployee(0).strFirst = "Anne" sEmployee(0).strLast = "Arkey" sEmployee(0).decPayRate = 7.5D sEmployee(0).decHours = 40 4. Make For/Next Loop For i = 0 To 4 Employee(i).decGross = Employee(i).decHours * Employee(i).decPayRate Next i OBJECTS AND CLASSES Base (parent) class : class that’s used as the basis for the other classes Business Logic Tier(controller): BUSINESS OBJECTS includes validation, calculations, business logic, business rules •Data Tier (model): DATA RETRIEVAL includes data storage

Upload: ryan-tran

Post on 26-May-2017

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exam 3 Review

ARRAYS AND STRUCTURES•Array: a set of related data, a list of similarly typed data, a group of related variables with the SAME data type •Element: a variable in an array•Index: unique number starting at zero assigned to each element of an array•Member: is the name of the typed variable; a variable in a structure•Populate: to assign data to the elements of an array•Structures: have DIFFERENT data types; a user-defined data type; an object that contains related variables often of mixed data types•Subscript: the element number of an array; ex: strState(39), 39 is the subscript of the array•Two-Dimensional Array: array of a single type that contains rows and columns of related data•Zero-Based: an array that numbers its elements starting at zero Concepts: ARRAYS: •An array is a series of related variables.•All variables in an array share the same name and data type.•Array variables are distinguished by their index and each item in an array has an unique

index number. The unique number for each element in an array is called a subscript.•Arrays are zero-based.•All arrays must be declared before they can be used and can be declared with a specific

size or no size.•Arrays can be populated when created or assigned values later•Arrays are easily manipulated with loops.•Arrays can be populated from a file or through user input.•An array can be resized at any time using ReDim. However, unless you use the Preserve

keyword, the existing data will be lost when you resize the array.•A two-dimensional array works well for sets of related data.•An array of type structure can be used to work with multiple records.• Structures are similar to records in a database – they store multiple types of related data in

a single container. Unlike arrays, the data types in a structure do not have to be the same.

-A structure MUST be declared outside of other subs, functions and event handlers.- Use s as the prefix for a structure to make recognition easier.ARRAY SYNTAX: Dim strPaint(9) as string; array name is strPaint, array stores 10 items, ea. Item is a string, array starts empty and # in parenthesis must be a integer or a variable that represents an integerPRE-POPULATING ARRAY: Dim varName( ) As Type = { List } use curly brace; dim creates an array, varName is the name of the array, type is data type, list provides a comma separated list to populate the array; ***Leave parenthesis empty when providing a list of items for an array

SINGLE ARRAY:*EXPLICITLY INITIALIZE/PUT VALUES IN ARRAY OR PUT VALUES IN A SEPARATE FUNCTONDim decExams ( ) As Decimal { 82, 84, 92} Dim decExams(2) as Decimal •leave parentheses blank! decExams (0) = 82 decExams(1) = 84 decExams (2) = 92

TWO-DIMENSIONAL ARRAY: Dim intPoints(4, 3) As Integer; intPoints: array that stores integers; 5 rows and 4 column•Structures are similar to records in a database – they store multiple types of related data in

a single container. •Unlike arrays, the data types in a structure do not have to be the same.•A structure MUST be declared outside of other subs, functions and event handlers and must

be created and declared before it can be used•Use s as the prefix for a structure to make recognition easier.EXAMPLE: Dim decGrades (119, 17) As DecimaldecGrades.GetUpperBound (0) gets you 119 decGrades.GetUpperBound (1) gets you 17• zero will get you the row and the one will get you the column

***DRAWBACKS OF TWO-DIMENSIONAL ARRAYS1. Entire array must be of the same data type2. Reference by 2 indices (row, column); not self-documenting -fix these 2 problems with structures!

STRUCTURES: 1. Create a structure OUTSIDE of event handlerPublic Structure sEmployee Dim strLast As String Dim strFirst As String Dim decPayRate As Decimal Dim decHours As Decimal Dim decGross As DecimalEnd Structure2. Declare Structure • Can create a single structure: Dim sEmployee As Employee OR • Create multiple structures Dim sEmployees(9) As Employee ***10 Employees created 3.Referencing Structures & Populate Structure: Structure.Member; structure is name of

the structure and member is name of the typed variable sEmployee(0).strFirst = "Anne"sEmployee(0).strLast = "Arkey"

sEmployee(0).decPayRate = 7.5DsEmployee(0).decHours = 404. Make For/Next Loop For i = 0 To 4 Employee(i).decGross = Employee(i).decHours * Employee(i).decPayRateNext i

OBJECTS AND CLASSES•Base (parent) class: class that’s used as the basis for the other classes•Business Logic Tier(controller): BUSINESS OBJECTS includes validation, calculations, business logic, business rules•Data Tier (model): DATA RETRIEVAL includes data storage•Derived (child) class: extension of a base class; a class that inherits properties and methods from a base class•Encapsulation: hides features and complexity of a class; protects others from it; gives access to only permitted parts by the author; hiding the data and features of class so that others cannot see its complexity•Exposed: term used to describe where an object is visible and can be used in a program•Inheritance: ability to create a class that’s based on another class; properties and methods of one class can be incorporated into new objects ex: FV sheep FV shamrock sheep•Instance Property: separate memory location for each instance of the object•Instantiate: to create a new instance of an object•Methods: subs and functions in a class, methods execute code and calculate and return values; same syntax as subs and functions•Object: a programming element that contains data(properties) and methods•Presentation Tier (view): USER INTERFACE includes forms, controls, menus •Property: describes a control•Shared Property: single variable that is available for ALL objects of a class, can be accessed without instantiating an object of the class, similar to module-level variables for adding to running totalsConcepts:•There are three tiers in most business applications. This architecture is sometimes referred

to as a model-view-controller pattern. The presentation (view) tier contains all the logic for interacting with the user (controls on the form, etc.). The business logic (controller) tier contains all of the logic related to business rules (calculations, workflows, etc.). The data (model) tier contains all of the logic related to reading, writing, updating and deleting data (presumably in a database).

•Object-oriented programming creates a “filing system” for your code, which allows you to separate code into the three tiers. This is useful, since most IT professionals are more skilled with some tiers than others. When the code is separated in this way, it is easy for subject matter experts to work on separate pieces of the code.

•Classes can be exposed in several ways, using Public, Private, and Friend.•Classes have properties that are used to store data.•Property values are stored (written) with a Set command and accessed (read) with a Get

command. When a property is ReadOnly, there is a Get with no Set. When a property is WriteOnly, there is a Set with no Get.

•An instance property is a property where each object created from the class has a separate occurrence of the property. A shared property is a property that is used by all instances (objects) of a class. Only one copy of a shared property exists for all instances (objects) of the class. Shared properties are generally used for counters and accumulators.

•Class methods are similar to procedures (subs) and functions.•Instantiation creates a new instance of a class. Classes must be instantiated before they

can be used. (Put the pig on the farm!)•All classes should be documented so a programmer can figure out how to use them.•Why use a class? Because, good for large projects, projects can be segmented which

makes them easier to work with and easy to assemble, maintenance is easier and automatically incorporated into all places where its used, planning is important when using classes, and code is reusable.

Why we write OO: If we don’t know this then we do not know the street skills like other programmers.

I. Write a ClassA. Write properties—adjectives, data, state of class

1. Declare PRIVATE module level variablesPrivate/Dim decWidth As Decimal

2. Property Block ( Public/Friend) Public/Friend Property Width As Decimal

GetReturn decWidth

End GetSet ( ByVal value as decimal)

decWidth=valueEnd Set

End Property

B. Methods (subs and functions aka verbs and actions of a class) Public/Friend Function CalcArea( ) As Decimal

Return decWidth*decLengthEnd Function

II. Use the ClassA. Declarations: Instantiate class/must use instance of class to use it

Dim CalculationObject as New CalculationsClassNOTE: CalculationsClass is the name of the class you already created;

CalculationsObject is the name of the instance of that class you are creating and has all the properties and methods of the original Calculations Class

B. Inputs: populate input properties (setters)

Page 2: Exam 3 Review

CalculationsObject.Width = 6DC. Processing—call subs and functions

decArea=CalculationsObject.CalcArea ( ) D. Ouputs- read output properties

lblWidth.Text = CalculationsObject.Width.ToString