0

Have we event handler,event listener,action listener or any like these, for arrayin any compile-based language (e.g. Java,c++,c)?

I am searching a way to track the changes of an array. So when value of a cell changed the listener notify me about this change and the cell number that this changed occurred for it.

is there any language or any option for this problem?

3
  • You can implement it in whatever language you like that support events C# does that very well in my opinion Commented Nov 8, 2013 at 8:44
  • you mean that I can define event listener for array?? You know, I asked some question like this yesterday about Java here: link but every one said it is impossible. Commented Nov 8, 2013 at 8:49
  • not for an array object but you can implement it using a custom type or any specialized collections why wouldn't you want to do that ? Commented Nov 8, 2013 at 9:08

1 Answer 1

1

Edit

If you do not want to use a custom type you can have a look at this implementation to observe when an array is changed : http://www.codeproject.com/Articles/162966/Observing-Changes-to-an-Underlying-Array

Here is an example from Microsoft in C# that does what you are looking for with an ArrayList

http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

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

   // 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();
      }
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Well here we have list and we can track the changes on a list, but I want to track the changes of an array.
Yes that's true I guess because array are primitive types they are not listenable but why couldn't you do that with a custom type ?
Well in my program, I have rigid space-constraint, so using list instead of array is not suitable from this point.
Then maybe the reference I provided to this article codeproject.com/Articles/162966/… may help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.