Cis166 Final Review C#

Post on 14-May-2015

584 views 1 download

Tags:

transcript

CIS-166 FinalCIS-166 FinalOpen book, open notes, open

computer100 pointsTrue/false, multiple choice, fill-in,

short answerEmphasis on material since

midterm

ArraysArraysList or series of values all

referenced by the same nameUse an array to keep a series of

variables for later processing such as ◦Reordering◦Calculating◦Printing

Array TermsArray Terms

Element◦Individual item in the array

Index (or subscript)◦Zero based Zero based number used to

reference the specific elements in the array

◦Must be an integerBoundaries

◦Lower Subscript, 0 by default◦Upper Subscript

Simple Array ExampleSimple Array Example

studentNames Array(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)

Janet BakerGeorge LeeSue LiSamuel HoosierSandra WeeksWilliam MacyAndy HarrisonKen FordDenny FranksShawn James

Dim Statement for ArraysDim Statement for ArraysDefault ValuesDefault Values

string[] strName = string[4]Results in an array of 4 elements:

strName(0), strName(1), strName(2), strName(3)

decimal[] decBalance= new decimal[100]Results in an array of 100 elements:

decBalance(0), . . . , decBalance(99)

Dim Statement for ArraysDim Statement for ArraysAssigned ValuesAssigned Values

string[] departments = {"ACT", "MKT", "HR"}Results in an array with 3 elements, each with a value, departments[0] is “ACT”

Integer[] intActCode = {10, 20, 30, 40}Results in an array with 4 elements, each with a number stored

Referencing Array Referencing Array ElementsElementsUse the Index(es) of the Element

strName(row)(0)(1)(2)(3)

Sam SmithJill CreechPaul FryRich Wells

strName[0] : "Sam Smith"strName[1] : "Jill Creech"strName[2] : "Paul Fry"strName[3] : "Rich Wells"

Working with ArraysWorking with ArraysUse Loops to reference each

element in the array◦For / Next ◦For Each / Next

For Next LoopFor Next Loop

Assume strNames[10] already declared

integer intCounter, intEndintEnd = strNames.GetUpperBound(0)For (intCounter = 0; intcounter<=intEnd;

intCounter++){Console.Writeline(strNames[intCounter]

)}

For Each LoopFor Each Loop

Assume strNames[10] already declared

foreach (string Item in strNames){Console.Writeline(Item)]

Object Terminology Object Terminology ReviewReviewObject - like a noun, a thing

◦An object is based on a classProperties - like an adjective,

characteristics of objectMethods - like a verb, an action

or behavior, something the object can do

Events - object response to user action or other events

PolymorphismPolymorphismOverloading: Argument type

determines which version of a method is used◦Example: MessageBox.Show method

Overriding: Refers to a class that has the same method name as its base class◦Method in subclass takes precedence

Specifying a NamespaceSpecifying a NamespaceNamespaces are used in .Net to

organize classes and source filesWhen referring to classes in a

different namespace◦ Write out the entire namespace◦ Add an Imports Statement to include the

namespaceUsing System.Windows.Forms.Form

Name of the ClassNamespace

Instance versus Static Instance versus Static VariablesVariablesInstance variables or properties

use a separate memory location for each instance of the object

Static variables or properties use a single memory location that is available for ALL objects of a class◦Can be accessed without

instantiating an object of the class◦Use the Static keyword to create

Static Methods can also be created

Constructors and Constructors and DestructorsDestructorsConstructor: Method that

automatically executes when an object is instantiated◦Create by writing a procedure using

the Class Name, followed by any arguments

Destructor: Method that automatically executes when an object is destroyed◦Microsoft discourages use of in .Net

CollectionsCollectionsGroup of objects

◦Can be strongly typed: all objects based on the same class

Similar to an array◦Collection expands and contracts

automaticallyHave common properties and

methods◦Add, Remove, Count, Item (Indexer)

Item PropertyItem PropertyTypically default property for a

collection◦Refer to collection object, followed

by location (in [])Returns a member of the group

◦Typically based on location, but can use other values

◦Data type depends on the type of objects the collection manages

Text Data FilesText Data Files

Actual data stored in files on disk device

File: Entire collection of dataRecords: Rows or lines, one

per entityFields: Data elements (values)

within a row

Text File HandlingText File Handling

A Stream is designed to transfer a seriesof bytes from one location to another

Streams are objects that have properties and methods

Found in the System.IO namespaceFile handling projects usually

contain an Imports statement before the statement declaring the form's class

Writing Data FilesWriting Data FilesDeclare a new StreamWriter

objectUse StreamWriter's WriteLine method

Call StreamWriter's Close method

Write and WriteLine Write and WriteLine MethodsMethodsWrite Method: Places items

consecutively in the file with no separator

WriteLine Method: Places an Enter (carriage return) between records

Reading FilesReading FilesDeclare a new StreamReader

object◦File must exist!

Use StreamReader's ReadLine method◦Loop to retrieve multiple records

Call StreamReader's Close method

ReadLine MethodReadLine Method

Use to read previously saved dataEach time it executes, it reads the

next line of dataAlways assign the value from the

read to a location, such as a label, text box, or string variable

Checking for End of FileChecking for End of FileUse StreamReader's Peek Method

Peek looks at the next element without actually reading it

If you Peek beyond the last element the value returned is -1