cop 2360 – c# programming final review. programming language basics a way to store and retrieve...

30
COP 2360 – C# Programming Final Review

Upload: debra-marilyn-higgins

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

COP 2360 – C# Programming

Final Review

Page 2: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Programming Language Basics

A way to store and retrieve information to and from memory.

A way to communicate with the "outside world" A way to compute basic mathematics and store

results A way to compare information and take action based

on the comparison A way to iterate a process multiple times A way to reuse components.

Page 3: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

A way to reuse components.Methods

A method is a group of code that (may or may not) accept parameters and (may or may not) produce a result

We have been writing a Methodstatic void Main(string[] args)method which– Accepts a parameter named args that is a string array– Returns nothing– Is a Static Method – Is the starting place for our programs

Another name for “Method” could be:– Function (what a method is called in C and C++)– SubProgram (what a method with nothing to return is called in

VB.Net)

Page 4: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

The Concept of Methods

Methods are usually blocks of code that are accessed multiple times in a program.

– So they allow for code to be reused – They accept parameters so that their results are flexible.

Methods with the same name can have different sets of parameters and can return different types. This feature is called overloading.

Methods are members of Classes. Methods that return a value can be used as a

variable on the right side of an expression (like a constant)

Page 5: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Classes

Classes are structures that combine data and processing into one component and are the basis of object oriented programming

All data types in C# are really instances of classes.– Strings are a good example of the abilities of

classes with all of the methods available to anything that is typed a String

Page 6: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Classes in C#

Classes are composed of Attributes (variables), Properties (values) and Methods (functions and subroutines.)

It includes a special Method called a Constructor which is used to initialize the class to a state where the properties and methods can be accessed.

Page 7: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Scope of Variables

Global/Class/Module– Variables defined before any method definitions are made– Should be used VERY SPARINGLY– Can be accessed by any method at any level within a class.

Local/Method– Variables defined within a method (but not inside an if, while, do, for or any other type

of block.)– Can only be accessed by that method . Another method cannot “see” the variables in

the first method . – Once the method returns, all local variables are destroyed.– By Value Parameters create local variables – Local Variables can hide Global Variables, but the Global Variables can be accessed

by prefacing them the class name. Block

– Variables defined within an if, while, do, for or any other type of block {}– they exist only for the life of the block, and are destroyed when the block is

completed.– C# will not let Block Variables “hide” Local Variables

Page 8: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

What’s an Array

An array is a collection of variables of the same data type.

The array is referenced by a single name suffixed with an index pointer.

Page 9: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Array Basics

When declared, memory is set aside based on the variable type and number of elements in the array

– int[] nPrices = new int [100]Will allocate an array named nPrices with 100 elementsThe elements are indexed from 0 to 99:

Each element can hold one integer value The above example is a “one dimensional” array. It may be easier to think that the computer is

creating 100 separate variables named nPrices[0] through nPrices[99] (or maybe not)

Page 10: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Array Basics

Normal arrays sizes are static.– That is, they cannot be changed once initialized.– You can use constants or variables in the definitions

const int nARRAY_SIZE = 20; int[] nMyArray = int[nARRAY_SIZE];

– Other collections such as an ArrayList allows a more dynamic approach and their size can be changed.

Arrays can be initialized when they are declared– char[] sLetterGrade = new char[5] {‘A’,’B’,’C’,’D’,’E’};– int[] nScore = new int[5] {0,0,0,0,0}; // initializes ALL elements

to 0– int[] nDemo = {3,6,9}; //Creates an array with three elements

Page 11: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Array Basics

Supposed we have two arrays– int[] nArrayOne = {1,2,3,4,5};– int[] nArrayTwo = new int[5];

We would like to copy nArrayOne to nArrayTwo– But we cannot do it directly!!– We must copy element by element.– Same is true for comparison, printing, etc.– Also, as parameters to methods, arrays are always By Ref

Page 12: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

The String Class (is really the application of an Array)

Stores a collection of Unicode characters into an “invisible” array Reference type

– Normally equality operators, == and !=, compare the object’s references, but operators function differently with string than with other reference objects

Equality operators are defined to compare the contents or values

Ditto for Assignments. With a regular Array, one cannot say – Array1 = Array2

Since this will assign make the two arrays reference the same spot in memory. For Strings, the assignment will take the value of the second expression, create a new array for the string, and place the value into that new array.

Strings are immutable – Making a change isn’t making a change, it’s making a new string!!

Page 13: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Parallel Arrays

Sometimes you may have two or more arrays that are linked in someway– State abbreviations to State Names for example.

String[] sStateAbbrevs = new String[50]; String[] sStateNames = new String[50];

– Now, we can have a user enter the two digit state abbreviation, use our look-up logic from a couple of slides ago, and find the abbreviation. Once we have the abbreviation, we are also pointing to the name.

Page 14: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

C# Programming: From Problem Analysis to Program Design 14

ArrayList Class

Limitations of traditional array– Cannot change the size or length of an array after it is

created

ArrayList class facilitates creating listlike structure, AND it can dynamically increase or decrease in length

– Similar to vector class found in other languages

Includes large number of predefined methods

Page 15: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

ArrayList Class (continued)

Any predefined or user-defined type can be used as an ArrayList object

C# also includes a List<> class– List<> class requires that objects be the same

type when you place them in the structure– ArrayList allows you to mix types

C# Programming: From Problem Analysis to Program Design 15

Page 16: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

ArrayList Class (continued)

using System.Collections;

ArrayList anArray = new ArrayList(); // Instantiates ArrayList

anArray.Add("Today is the first day of the rest of your life!");

anArray.Add("Live it to the fullest!");

anArray.Add("ok");

anArray.Add("You may not get a second chance.");

anArray.RemoveAt(2); // Removes the third physical one

for (int i = 0; i < anArray.Count; i++) //Displays elements

{

Console.WriteLine(anArray[i]);

}

Console.ReadKey();

}

16

Page 17: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Array vs Arraylist

Array– Fixed length. Once defined

the size cannot be changed.

– Elements must all be of the same base type. (That is, Strongly Typed!!)

– Much faster processing than ArrayList since it does not have to worry about data type nor tracking adds and removes.

ArrayList– Variable Length. One can

add and remove elements from the array.

– Elements can be of different types, but this comes at an overhead.

– Elements are all “typed” as base Object and then must be cast to the correct data type.

List has similar properties to ArrayList, but is strongly typed.

Page 18: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Console Apps vs. Windows Apps

Console Apps have a specific start and end with usually minimal “outside” interaction.

– Many times used for batch processing– Although there can be different processing paths depending upon

input or data, the paths are relatively fixed. Windows Apps are reliant on the user “doing” something on a

form.– Entering data, pushing a button, selecting from a drop-down.– Every execution is potentially different because it all depends upon

the interaction of the user.– The program basically sits there and waits for the user to do

something.– Each control on the form has many events that it can react to.

Each event can have a different set of code to process that event.

Page 19: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Windows Form Types

Different Types of Windows Apps – WinForms – Older “heavy client” applications. – Windows Presentation Foundation (WPF) – Newer graphical “heavy client”– ASP.Net – Web Based with Server Side Processing– Silverlight – Web/Browser Based implementation of WPF

All are “action” based. In other words, they sit there and wait until the user does something (or doesn’t do something), which in turn kicks off specific code to execute.

All are object oriented in that all forms and controls (those widgets you stick on forms) are instances of classes with properties, attributes and methods.

All add another structure to a class object called a “event handler” which is method that is invoked when a user does something to a specific control.

Page 20: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Different Types of Windows Apps

WinForms and WPF– Process on the client. Each client must have their

own executable program

ASP.Net and SilverLight– For the most part, process on a Server that runs

the actual code and database access. It then sends back HTML code once the process is

complete The HTML code then runs in a Web Browser

Page 21: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Controls

Controls are the widgets you place on a form that the user interacts with.

Properties of a control describe how that specific control is to look or function on a particular form.

– All forms have certain properties that they inherit from their parent classes

– Specific controls then have their own set of properties in addition to the inherited ones.

Properties can include the text displayed, the font, the color, the location and size and a myriad of other adjectives of the control.

Page 22: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

What Controls are Available in Windows Forms?

Text Controls– Labels

A control that cannot take user input but can be manipulated programmatically. Used for titles or messages.

– Text Boxes A control that may or may not allow input (depending upon the way it is

set up.) May be single line or multi-line

– Masked Text Box Allows developer to define the format of the data for entry

– Rich Text Boxes Has the abilities of a regular text box PLUS users can change formats

and fonts (like they can do in Word) (but you have to program it be able to do that!!)

Page 23: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Selection Controls we have Used

Selection Controls– Radio Buttons – Can select exactly one from a group.

To allow for multiple series of Radio Buttons, can be placed inside of other controls.

– Panels and Group Boxes.

– Check Boxes – Can select none, all or anything in between– Drop-Down Lists/Combo Boxes

Can usually select only one, but does allow for multiple selections.

Requires one to load values to the list.

Page 24: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Other Controls we have Used.

Grouping– Panels – A container that can hold other controls.– Group Box – Another type of container that includes a heading.– Tabs – A container that uses tabs to separate content.

Program Flow– Menu – A multi-level menu where each leave of the menu has its own set

of possible events.– Tool Strip – A list of icons similar to what’s available in Microsoft Office– Timer – A control that kicks off a “tick” event in specific increments.– Button – A control a user would “click” to initiate an action.

Other– Picture Box– Open File Dialog – A special control that allows a user to pop-up a

Windows Explorer type interface to select a file.

Page 25: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Dynamically Adding Controls to our Forms

Although the main way most forms are included on a Windows Form are added by dragging and dropping the control from the tool box to the form, controls can be added on the fly.

– The control is given a name and a type and the “new” type is invoked to call the constructor for that control

Button btnNew= new Button;which initialized the properties of the new control

– Once the new control is created, the programmer must then set all the properties to what is needed (height, width, top, left, text, etc.) add then “add” the new control to the parent control (or this if it goes to the form).

this.Controls.Add(btnNew);– Then, the new control can be associated with Event Handlers to be

triggered when the user does something to that new control: btnNew.Click +=new EventHandler (btnNew_Click);

In this example, btnNew_Click is the name of the method that will be called when a user presses the button.

Page 26: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Some Standard Properties of a Control

Text – What is displayed as the text, label, heading etc of the control. Enabled – Whether or not the user can interact with the control. Setting a

parent control to Enabled = False will also set all child controls to Enabled = False

Visible – Whether or not the control is shown in the form. Location (Top, Left) – The coordinate of the top, left corner of the control.

Basically, where the control is being painted on the form. Size (Height, Width) – How tall and wide a control is. Tooltip – What is displayed when a user “hovers” over a control. Requires the

ToolTip control to be added to the form. Font – Used to change the size and font for the text of a control. Forecolor – Used to change the color of the text of a control. Backcolor – Used to change the background color of the control. Tabindex – The order in which the tab key jumps from control to control. Tabstop – Whether or not the tab key will send the cursor to the control.

Page 27: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Message Box

MessageBox is a class with a set of static methods that allow a programmer to display a message and can also allow the user to respond in a limited way by pressing specific button.

The basic method call is:– MessageBox.Show(“Text Message”)

But, MessageBox has many different overloads that can additionaly specify a form heading, an icon and a set of buttons.

– When we use the MessageBoxButtons, the MessageBox.Show method can return a value

The value return is an “enumeration” type – which basically is an integer that has been given a “name”

The variable to which it is assigned is of type var It is compared to attributes of the DialogResult class When selecting what to display, the MessageBoxButtons class has several

selections like “OK”, “OK Cancel”, “Yes No” and “Abort, Retry, Ignore”

Page 28: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Control Event Handlers

Event Handlers are the links between what a users “does” to a control and what code is executed for that event.

Each control has a default “Event Handler” (usually click) that you can get to by simply double clicking the control.

But, there are many other events that can also be identified and handled.

– For a specific control, they are listed under the “lightening bolt” properties for that control.

Page 29: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

A Word on Event Handlers

Once an Event Handler is Written, it can be called by many different events

You can also have common code created in a separate method with event handlers for each unique event, and the event handlers then call the common code.– I’m for the second way!!

Page 30: COP 2360 – C# Programming Final Review. Programming Language Basics A way to store and retrieve information to and from memory. A way to communicate with

Event Handler Information

What is this “sender as object” thing?– The event handler has no clue what was done to trigger the

event, only that it was called.– The “sender” parameter then is a reference to the control

that caused the event to be fired. You can cast sender to different type of controls and then

retrieve the information about that control. What is this “e as EventArgs” thing?

– Certain events may return arguments depending upon how they were fired.

– We’ll look at one when we get to Web ASP.NET Development