Hi Guys
NP112 this[int index]
http://msdn.microsoft.com/en-us/library/aa645739(VS.71,printer).aspx
Following program is given in the above website. Code highlighted in green is new to me (this[int index]).
Please explain it.
Thank you
// events1.cs
using System;
namespace MyCollections
{
using System.Collections;
// A delegate type for hooking up change notifications.
public delegate void ChangedEventHandler(object sender, EventArgs e);
//carry the event data
// A class that works just like ArrayList, but sends event
// notifications whenever the list changes.
public class ListWithChangedEvent : ArrayList
{
// An event that clients can use to be notified whenever the
// elements of the list change.
public event ChangedEventHandler Changed;
// Invoke the Changed event; called whenever list changes
protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this, e);
}
// Override some of the methods that can change the list;
// invoke event after each
public override int Add(object value)
{
int i = base.Add(value);
OnChanged(EventArgs.Empty);
return i;
}
public override void Clear()
{
base.Clear();
OnChanged(EventArgs.Empty);
}
public override object this[int index]
{
set
{
base[index] = value;
OnChanged(EventArgs.Empty);
}
}
}
}
namespace TestEvents
{
using MyCollections;
class EventListener
{
private ListWithChangedEvent List;
public EventListener(ListWithChangedEvent list)
{
List = list;
// Add "ListChanged" to the Changed event on "List".
List.Changed += new ChangedEventHandler(ListChanged);
}
// This will be called whenever the list changes.
private void ListChanged(object sender, EventArgs e)
{
Console.WriteLine("This is called when the event fires.");
}
public void Detach()
{
// Detach the event and delete the list
List.Changed -= new ChangedEventHandler(ListChanged);
List = null;
}
}
class Test
{
// Test the ListWithChangedEvent class.
public static void Main()
{
// Create a new list.
ListWithChangedEvent list = new ListWithChangedEvent();
// Create a class that listens to the list's change event.
EventListener listener = new EventListener(list);
// Add and remove items from the list.
list.Add("item 1");
list.Clear();
listener.Detach();
}
}
}
/*
This is called when the event fires.
This is called when the event fires.
*/
Posted Aug 2, 2008, 7:36 PM
Thank you for your explanation.
AlanPosted Aug 2, 2008, 7:28 PM
In this code:
public override int Add(object value)
{
int i = base.Add(value);
OnChanged(EventArgs.Empty);
return i;
}
'value' isn't in fact used as an integer.
The ArrayList.Add() method takes an object parameter and returns an int which represents the index at which the object has been added to the ArrayList. So 'i' is the index at which 'value' has been added and is then returned to the caller.
Usually, when the ArrayList.Add() method is called the return value isn't of interest and so is discarded.
Posted Aug 2, 2008, 4:41 PM
In the above program variable value in the Add() method header (highlighted in yellow) is an object data type. How can it be an integer (int i = base.Add(value);) inside the method. Please explain.
AlanPosted Aug 1, 2008, 7:23 PM
This declaration:
public override object this[int index]
{
set
{
base[index] = value;
OnChanged(EventArgs.Empty);
}
}
declares an indexer for the ListWithChangedEvent class where the setter overrides the one which it inherits from its parent ArrayList class.
It's used in exactly the same way as ArrayList's indexer to provide access to the members of the collection in the order they were added.
An indexer is always declared with the keyword 'this' followed by the index to be used in square brackets.