neal stublen [email protected]. what’s an indexer? an array has an indexer… int[] myarray = new...
TRANSCRIPT
![Page 2: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/2.jpg)
INDEXERS
![Page 3: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/3.jpg)
What’s an indexer?
An array has an indexer…
int[] myArray = new int[5];for (int index = 0; index < 5; ++index){ myArray[index] = 3;}
The [ ] is the indexer.
![Page 4: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/4.jpg)
What’s an indexer?
An indexer accesses one value by using another value
A special property: public <type> this[<index_type> <index_value>]
The index_type is often an integer, but can be another data type
Implemented on collection classes Throws an ArgumentException
ArgumentNullExceptionArgumentOutOfRangeException
![Page 5: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/5.jpg)
Indexer Exampleclass MyThing{}
class MyCollection{ public void AddThing(MyThing inThing) { ... }
public MyThing this[int index] { get { ... } set { ... } }}
![Page 6: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/6.jpg)
DELEGATES AND EVENTS
![Page 7: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/7.jpg)
Start with an Example
![Page 8: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/8.jpg)
What’s a delegate?
A delegate specifes a method signature for an event
A special data type:public delegate <return_type> DelegateName([parameters_list]);
![Page 9: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/9.jpg)
Delegate Example// The delegate type declarationpublic delegate void NameChangedEventHandler(object sender, EventArgs e);
class NameObject{ // The event member public event NameChangedEventHandler NameChanged;
public void setName(string inName) { mName = inName; if (NameChanged != null) { NameChanged(this); } }}
![Page 10: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/10.jpg)
Connecting to an Eventclass SomeObject{ private NameObject mTheName = new NameObject();
public void Init() { mTheName.NameChanged += new NameChangedEventHandler(EventHandler) }
private void EventHandler(object sender) { }}
![Page 11: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/11.jpg)
OPERATOR OVERLOADING
![Page 12: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/12.jpg)
What operations?
Unary operators+, -, !, ++, --, true, falsepublic static <return_type>operator <operator>(<operand_type>)
Binary operators+, -, *, /, %, &, |, ==, !=, >, <, >=, <=public static <return_type>operator <operator>(<operand_type_1>,
<operand_type_2>)
![Page 13: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/13.jpg)
Operator Exampleclass MyCollection{ private List<object> mList;
public static MyCollection operator +(MyCollection col, object newObject)
{ col.mList.Add(newObject); return col; }}
collection += new object();
![Page 14: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/14.jpg)
Overloading ==
Must also overload Equals() Must also overload GetHashCode() Must also overload !=
Must overload relational operators in pairs (<, >), (<=, >=), (==, !=)
![Page 15: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/15.jpg)
Practice Exercise
Exercise 13-1, p. 418
![Page 16: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/16.jpg)
CLASS INHERITANCE
![Page 17: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/17.jpg)
What is inheritance? One class inherits the attributes and
behaviors of another class The base class should be a larger
classification of the derived class (ex. a Control is a larger classification for Button)A Button “is-a” Control; a Button “has-a”
BackColorA Book “is-a” Product; a Book “has-a” Publisher
The derived class extends or overrides behavior
![Page 18: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/18.jpg)
.NET Inheritance
All classes implicitly inherit from System.ObjectGetType()ToString()Equals()ReferenceEquals()GetHashCode()Finalize()MemberwiseClone()
![Page 19: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/19.jpg)
![Page 20: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/20.jpg)
How does inheritance work?
class Fourth class Third class Second class First
methodA
methodB methodB
methodC methodC methodC
methodD methodD
methodE methodE
Fourth fourth = new Fourth();fourth.methodA();fourth.methodB();fourth.methodC();fourth.methodD();fourth.methodE();
First first = new Fourth();first.methodA();first.methodB();first.methodC();first.methodD();first.methodE();
“Polymorphism”The base class can take many different
forms.
![Page 21: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/21.jpg)
Polymorphism
List<First> myList = new List<First>();myList.Add(new First());myList.Add(new Second());myList.Add(new Third());myList.Add(new Fourth());
foreach (First item in myList){ item.methodB(); item.methodC();}
![Page 22: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/22.jpg)
How do we “do” inheritance?
Keywords: virtual, override A base class declares a method as
virtual A derived class declares a method as
override public/private =>
public/private/protected/internal Reference base class using “base.”
![Page 23: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/23.jpg)
Inherited Classesclass First{ public virtual void methodA() { }}
class Second : First{ public override void methodA() { base.methodA(); }}
![Page 24: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/24.jpg)
Considerations for Inheritance
Confirm “is-a” versus “has-a” relationship
Does adding one or more properties make sense?
Would an interface be more beneficial? Implicit and explicit casting between
inherited types Using the “as” operator instead of
casting to avoid exceptions
![Page 25: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/25.jpg)
Abstract Classes
Abstract classes cannot be instantiated, and can only serve as a base class
Abstract methods and properties must be overridden in a derived classYou know the method or property exists for
every object of this type, but there is no implementation at this level of abstraction.
![Page 26: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/26.jpg)
Sealed Classes
Sealed classes cannot be inherited Sealed methods and properties cannot
be overridden
![Page 27: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/27.jpg)
Start thinking about how objects you need to model may inherit from one another.
Are there any obvious heirarchies, common attributes, or shared behavior?
![Page 28: Neal Stublen nstublen@jccc.edu. What’s an indexer? An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {](https://reader036.vdocuments.net/reader036/viewer/2022062314/56649f135503460f94c27c1e/html5/thumbnails/28.jpg)
Suggestions
Try to work through Exercises 14-1 and 14-2 in the book (p. 457)