OOP Class7

esse quam videri
Revision as of 18:29, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
Jump to: navigation, search

OOP Arrays


  • OOP Class1 Introduction to .NET and C#
    • Compliled vs Interpreted Languages
  • OOP Class2 C# fundamentals
    • Basic Data Types
    • Operators
    • Conversion Casting
    • Constants
    • Arrays
      • Single Arrays
      • Multi Dimensianal Arrays
      • Jagged Arrays
      • Enumerators
  • OOP Class3 CSharp Control Structures
    • Operators
    • Branching Statements
      • Branching
        • If
        • Switch
      • Looping
        • for
        • foreach
        • do
        • while
  • OOP Class4 Classes and Objects
    • Encapsulation
    • Classes
      • Fields
      • Methods
      • Constuctor
    • Overloading
  • OOP Class5 Object Relationships
    • Access Modifiers
    • Accessors
    • Static Members
    • Relationships
      • Association 'Uses a'
      • Containment 'Has a'
      • Inhertance 'Is a'
  • OOP Class6 Object Relation Ships 2 / UML
    • UML
    • Inheritance 'Is A'
    • Virual Functions Override
    • Polymorphism
    • Structs
  • OOP Class7 Polymorphism / Visual Studio 2005
    • Fancy Arrays
    • Colections
    • Polymorphism

Cat app with multiple toys

Course App in console and windows


Lightweight alternatives to classes. Structs do not support inheritance or destructors. Don't worry if you don't understand structs yet it hard cuz the book teaches them before classes. We will talk more about stucts after we talk about classes Syntax

[ attributes] [access-modifiers] struct identifier [:interface-list {struct members}

1 struct Dog
2 {
3  public string name;
4  public string weight;
5  public int age;
6 }


Enumerators are used to set predefined list of named constants. Syntax

[ attributes] [modifiers] enum identifier [:base-type {enumerator-list};

1 //An enumerator for ServingSizes at BK
2 enum ServingSizes : uint
3 {
4   Small = 0,
5   Regular = 1,
6   Large = 2,
7   SuperSize = 3
8 }
1 //another more useful example
2 // forced sequence to start  
3 // from 1 instead of 0 (default)
4 enum Months 
5   {
6     January = 1, February , March, April ,
7     May , June , July , August , Sept , Oct , Nov , Dec 
8   }

Enumerator Example Enum.cs

Fancy Arrays


Regular Array objects are like list collections

Arrays are groups of variables of the same type

        type [] identifier

single dimension arrays

1 string [] aryNames = new string[5];
3 aryNames [0] = "Feta";
4 aryNames [1] = "Colby";
5 aryNames [2] = "Gruyere";
6 aryNames [3] = "Edam";
7 aryNames [4] = "Colby";

Arrays are actually objects and support various methods.

  • GetValue
  • IndexOf
  • LastIndexOf
  • SetValue
  • Reverse
  • Sort


1 string s = aryNames.GetValue(1).ToString();


1 int i =  Array.IndexOf(aryNames, "Colby");


1 int Lasti= Array.LastIndexOf(aryNames, "Colby");


1     aryNames.SetValue("Gouda", 1);
2     string setValue = aryNames.GetValue(1).ToString();

Now Array(1) == "Gouda"


Reverse reverses the array
1 Array.Reverse(aryNames);

The Reversed array looks like Reverse

0 Colby
1 Edam
2 Gruyere
3 Gouda
4 Feta


The Sort method sort the array. Arrays are sorted accourding to how objects compare to each other usually implemented using the IComparer interface
1 Array.Sort(aryNames);

The Sorted Array List Looks like

0 Colby
1 Edam
2 Feta
3 Gouda
4 Gruyere

Array Example 2


Pros and Cons of working with arrays pros

  • East to use
  • Fast to alter
  • Flast to iterate though
  • Specific type of elements ( single type)


  • Fixed size
  • Insterting is difficult


Collections are groups of objects. types of collections

  • set - no index
  • list - has index
  • map - has key/value pair

Set is like storing a bunch of objects in a bag. The order they are removed from the bag has nothing to do with the order they are removed. Set collections do not have a unique identifier.

List types of collections have an integer for an indexer. This gives them a unique identifier and an order.

Map types also have an index but it is not necessarily an integer. It could be anything unique.


The List of now part of System.Collections. In order to user it you must add a using statement

1 using System.Collections.Generic;

create an arraylist

1 List<String> myList =  new List<String>();

add and element or two

1 myList.Add("Feta");
2 myList.Add("Colby");
3 myList.Add("Gruyere");
4 myList.Add("Edam");
5 myList.Add("Colby");

This produces and List like


Insert an element

1 myList.Insert(1,"Gouda");

Now our ArrayList Looks like


Remove an element

1 //remove by index
2 myList.RemoveAt(1);
3 //remove by data
4 myList.Remove("Colby");		//removes first occurance of Colby

Now our List looks like


Pros and con of Generic Lists Pros

  • No fixed size
  • Insert and Remove


  • Slower than arrays
  • Inefficient with memory (when the array size exceeds the upper bound .NET doubles the size of the array.

ArrayList Example



Arrays and Collections of objects allow for polymorphism.


1 foreach (string n in aryNames)
2 {
3  aryDropDown.Items.Add(n);
4 }

Pair Programming

Pair Programming

We are going to pair off to do our next assignment. Many of the paired programming principles come from XP (http://www.extremeprogramming.org/ Extreeme Progamming)

Pairs are good because

  • Pairs keep each other on track
  • A partner can help when you are stuck
  • Keep each other accountable

Some XP Principles

Fail - If you are having trouble succeeding fail.

Baby Steps - Do the smallest thing that you possible can that moves you in the right direction

Integrate and communicate your code often. Try posting up your code and emailing or plan on meeting several times.

When two pair programmers are together one you should sit a one computer (yeah that right two of you at one computer) and one should type while the other watches and reflects. Feel free to slide the keyboard back and forth when someone get tired, stuck or has a new idea.

I would like you and your paired partner to create and demonstrate classes in c# from the diagram below.

Home Work

Practice for Medterm Exam OOP Students Classes Diagram

If you need to add any supporting methods or properties that are not on the diagram feel free.

CourseName:Object Oriented Programming CoureseNumber:36-2600
Hello my name is Jeff Meyers my ID is 47167
I'm teaching:
CourseName:Object Oriented Programming CoureseNumber:36-2600
Hello my name is Sean  my ID is 666
I'm taking:
CourseName:Object Oriented Programming CoureseNumber:36-2600
CourseName:Writing and Retoric CoureseNumber:42-1000
ConsoleApplicationExtreemeCourse.Pencil is sharp
ConsoleApplicationExtreemeCourse.Pencil is not sharp