Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain."— Presentation transcript:

1 Arrays & Enum & Events

2 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.

3 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] = 2 + 2 * i;

4 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.

5 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.

6 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)

7 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

8 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.

9 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 } };

10 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

11 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

12 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

13 Delegates & Events

14 Events An event notifies other objects that something special has happened Example: Button class offers a Click event Example: An e-mail application receives new e-mails. When a new e-mail arrives, user wants to be notified via message box or forward that e-mail 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

15 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)

16 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);

17 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

18 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; }

19 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); }

20 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); }

21 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; }

22 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.


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

Similar presentations


Ads by Google