Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Programming Based on Events
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Java Unit 9: Arrays Declaring and Processing Arrays.
OOP Languages: Java vs C++
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Object Oriented Software Development
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Array String String Builder. Arrays Arrays are collections of several elements of the same type E.g. 100 integers, 20 strings, 125 students, 12 dates,
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Java SE 8 for Programmers, Third Edition
 2009 Pearson Education, Inc. All rights reserved Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
ECE122 Feb. 22, Any question on Vehicle sample code?
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Object Oriented Programming.  Interface  Event Handling.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Introduction to Object-Oriented Programming Lesson 2.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Arrays.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 2005 Pearson Education, Inc. All rights reserved Arrays.
A DVANCED P ROGRAMMING C HAPTER 8: A RRAYS Dr Shahriar Bijani Spring 2016.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
C# for C++ Programmers 1.
Object-Oriented Programming: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Event Driven Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Advanced Programming Chapter 8: Arrays
C# Event Processing Model
MSIS 655 Advanced Business Applications Programming
Constructors, GUI’s(Using Swing) and ActionListner
Corresponds with Chapter 5
Presentation transcript:

Arrays & Enum & Events

Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain the same length once they are created. Length property gives the length of the array. static Array.Resize resizes an array. It takes two arguments—the array to be resized and the new length. Arrays are reference types—what we typically think of as an array is actually a reference to an array object. If you pass an array parameter to a method, it is passed by reference. The elements of an array can be either value types or reference types. For example, every element of an int array is an int value, and every element of a string array is a reference to a string object. We access an array element specifying array’s name and element’s index (position in the array). Index starts at 0 (zero). CLR performs bounds checking for you and throws IndexOutOfRangeException.

Examples See Arrays int[] c = new int[12]; string[] c = new string[10]; int[] c; c = new int[12]; for (int i = 0; i < array.Length; i++) Console.WriteLine("{0} {1}", i, array[i]); int[] array = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37}; const int ARRAY_LENGTH = 10; int[] array = new int[ARRAY_LENGTH]; for (int i = 0; i < array.Length; i++) array[i] = * i;

foreach The foreach statement iterates through the elements of an entire array or collection. foreach (type identifier in arrayName ) { ;... ; } type and identifier are the type and name (e.g. int number ) of the iteration variable. The type of the iteration variable must match the type of the elements in the array. The iteration variable represents successive values in the array on successive iterations of the foreach statement.

Example: foreach int[] array = {87, 68, 94, 100, 83, 78, 85, 91, 76}; int total = 0; // add each element's value to total foreach ( int number in array ) total += number; The foreach statement can be used with any collection.NET provides as long as the type implements the IEnumerable and IEnumerator interface.

Implicitly Typed Variables C# provides a new feature—called implicitly typed local variables—that enables the compiler to infer a local variable’s type based on the type of the variable’s initializer. To distinguish such an initialization from a simple assignment statement, the var keyword is used in place of the variable’s type. You can use local type inference with control variables in the header of a for or foreach statement. if myArray is an array of ints, the following foreach statement headers are equivalent: foreach (int number in myArray) foreach (var number in myArray)

Passing Arrays as Parameters To pass an array argument to a method, specify the name of the array without any brackets. For a method to receive an array reference through a method call, the method’s parameter list must specify an array parameter. When an argument to a method is an entire array or an individual array element of a reference type, the called method receives a copy of the reference. When an argument to a method is an individual array element of a value type, the called method receives a copy of the element’s value. To pass an individual array element to a method, use the indexed name of the array as an argument in the method call. Example: ArrayReferenceTest.cs

Multidimensional Arrays 2-dimensional rectangular array: An array with m rows and n columns is called an m-by-n array. Every element in array a is identified by an array-access expression of the form a[ row, column ]; A two-by-two rectangular array b can be declared and initialized as follows: int[, ] b = { { 1, 2 }, { 3, 4 } }; The initializer values are grouped by row in braces.

Jagged Arrays A jagged array is a one-dimensional array whose elements are one-dimensional arrays. The lengths of the rows in the array need not be the same. A jagged array with three rows of different lengths could be declared and initialized as follows: int[][] jagged = { new int[] { 1, 2 }, new int[] { 3 }, new int[] { 4, 5, 6 } };

Multidimensional Arrays A rectangular array can be created with an array-creation expression: int[, ] b; b = new int[ 3, 4 ]; A jagged array cannot be completely created with a single array- creation expression. Each one-dimensional array must be initialized separately. A jagged array can be created as follows: int[][] c; c = new int[ 2 ][ ]; // create 2 rows c[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 c[ 1 ] = new int[ 3 ]; // create 3 columns for row 1

Variable-length argument lists Variable-length argument lists allow you to create methods that receive an arbitrary number of arguments. The necessary params modifier can occur only in the last entry of the parameter list. Example: ParamArrayTest.cs

enum An enumerated type is a value type that defines a set of symbolic name and value pairs. For example: enum type identifying a single color: public enum Color { White, // Assigned a value of 0 Red, // Assigned a value of 1 Green, // Assigned a value of 2 Blue, // Assigned a value of 3 Orange, // Assigned a value of 4 } Other examples: days/months, cards types, states in a game, etc. etc. Let’s see example code: enum.cs

Delegates & Events

Events An event notifies other objects that something special has happened Example: Button class offers a Click event Example: An application receives new s. When a new arrives, user wants to be notified via message box or forward that to his fax machine or mobile phone. MailManager Mobile PhoneFax Machine 2) new mail arrives 1) register for notification 1) register for notification 3) Notify that new mail arrived

Step #1: Define a type that will hold any additional information that should be sent to receivers of the event notification When an event is raised, the object raising the event might want to pass some additional information to the objects receiving the event notification This additional information needs to be encapsulated in its own class derived from System.EventArgs See NewMailEventArgs (MailManager.cpp)

Step #2: Define the event member An event member is defined using the event keyword public event EventHandler name_event; public event EventHandler NewMail; The type of the event is EventHandler This means event receivers are supposed to implement and pass a function delegate with following signature: void MethodName(Object sender, NewMailEventArgs e); System.EventHandler is defined as: public delegate void EventHandler (Object sender, TEventArgs e);

Step #3: Receiver Objects Implement the EventHandler method A method that performs a task in response to an event is called an event handler. Event member in previous step defined what type of event handler it expects. This means event receivers are supposed to implement and pass a function delegate with following signature: void MethodName(Object sender, NewMailEventArgs e); See Fax and Pager in MailManager.cpp When the event is raised Fax::FaxMsg and Pager::SendMsgToPager methods are called

Step #4: Receiver Objects Registers for the Event The objects interested in receiving notification when the event happens, should add their event handler to the event See Fax and Pager constructor in MailManager.cpp public Fax(MailManager mm) { // Construct an instance of the EventHandler // delegate that refers to our FaxMsg callback method. // Register our callback with MailManager's NewMail event mm.NewMail += FaxMsg; }

Step #5: Define a method responsible for raising the event to notify registered objects that the event has occurred The class should define a protected method to be called internally when the event is to be raised. This method takes one parameter, NewMailEventArgs object, to include the information to be passed to the receivers of the event notification. If the event, NewMail is not null, the event will be raised by simply calling the event as a method. See OnNewMail in MailManager.cpp protected void OnNewMail(NewMailEventArgs e) { EventHandler temp = NewMail; if (temp != null) temp(this, e); }

Step #6: Define a method that translates the input into the desired event The class will usually take some input and translate it to the way the event wants. We could join Step#5 ( OnNewMail ) and #6 ( SimulateNewMail) depending on implementation. See SimulateNewMail in MailManager.cpp public void SimulateNewMail( String from, String to, String subject) { NewMailEventArgs e = new NewMailEventArgs( from, to, subject); OnNewMail(e); }

Step #7: Receiver Objects De-Registers themselves from the Event When the objects interested in receiving notification are done with listening the event or do not want to listen to the event anymore, should remove their event handler from the event See Fax and Pager in Unregister in MailManager.cpp // This method should be executed to have the Fax object unregister // itself with the NewMail event so that it no longer receives // notifications public void Unregister(MailManager mm) { // Unregister ourself with MailManager's NewMail event mm.NewMail -= FaxMsg; }

User Interface and Events GUI (Graphical User Interface)’s are event driven. When the user interacts with a GUI component, the event drives the program to perform a task. Enter key is pressed Mouse double clicked Mouse moves to an area The GUI controls, which are.NET classes, are like MailManager : they raise several events and send notifications to receivers. Your class just needs to register to these events and hence implement the event handler methods. We will see many examples in Windows Forms.