Sometimes we want to extend generic classes in c#. Here i have extended ArrayList in c# and overrided remove functions. I have also added Events to this class and if anyone interested you can go to my GitHub account and check.

Here is the extended class

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnimalList
{

    // A delegate type for hooking up change notifications.
    //public delegate void MyAnimals_AnimalRemoved(MyAnimalList collection, Animal item);
    public delegate void MyAnimals_AnimalRemoved(object sender, AnimalListEventArgs e);

    /// <summary>
    /// MyAnimalList class only accept Animal types
    /// </summary>
    public class MyAnimalList : ArrayList
    {
        // An event that clients can use to be notified whenever the
        // elements of the list change.
        public event MyAnimals_AnimalRemoved AnimalRemoved;

        // Invoke the Changed event; called whenever list changes
        protected virtual void OnChanged(object sender, AnimalListEventArgs e) 
         {
             if (AnimalRemoved != null)
                 AnimalRemoved(this, e);
         }

         // Override some of the methods that can change the list;
         // invoke event after each
         public void Remove(int value)
         {
             MyAnimalList list = this;
             if (value > list.Count)
             {
                 MessageBox.Show(value+" is not a valid index", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return;
             }
             AnimalListEventArgs args = new AnimalListEventArgs((Animal)list[value]);
             OnChanged(this, args);
             base.Remove(value);
         }

         // Override some of the methods that can change the list;
         // invoke event after each
         public override void RemoveRange(int from,int to)
         {
             ArrayList list = this.GetRange(from, to);
             foreach(var animal in list)
             {
                 OnChanged(this, new AnimalListEventArgs((Animal)list[list.IndexOf(animal)]));
             }
             base.RemoveRange(from,to);
         }

         // Override some of the methods that can change the list;
         // invoke event after each
         public override void RemoveAt(int value)
         {
             MyAnimalList list = this;
             OnChanged(this, new AnimalListEventArgs((Animal)list[value]));
             base.RemoveAt(value);
         }

        /// <summary>
        /// Override + operator 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="animal"></param>
        /// <returns></returns>
        public static MyAnimalList operator +(MyAnimalList list,Animal animal)
        {
            list.Add(animal);
            return list;
        }

        public new void Add(object value)
        {
            Type t = value.GetType();
            if (t.Equals(typeof(Animal)))
            {
                 base.Add((Animal)value);
            }
            else
            {
                MessageBox.Show("Only Animal type objects can be added to the MyAnimalList collection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }
    }
}

And also i have extended EventArgs class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnimalList
{
   public class AnimalListEventArgs:EventArgs
    {
       private readonly Animal animal;

       public AnimalListEventArgs(Animal animal)
       {
            this.animal = animal;
       }

       public Animal Animal
        {
            get { return this.animal; }
        }
    }
}

Here is my program class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnimalList
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("||-- Starting animal program -- ||");

            MyAnimalList MyAnimals = new MyAnimalList();
            MyAnimals.AnimalRemoved += new MyAnimals_AnimalRemoved(ListChanged);

            MyAnimals.Add(new Animal(AnimalType.Amphibian, "Frog"));
            MyAnimals.Add(new Animal(AnimalType.Bird, "Eagle"));
            MyAnimals.Add(new Animal(AnimalType.Fish, "Bass"));

            MyAnimals += new Animal(AnimalType.Invertebrate, "Worm");
            MyAnimals += new Animal(AnimalType.Mammal, "Lion");
            MyAnimals += new Animal(AnimalType.Reptile, "Snake");

            MyAnimals.Add("dog");

            MyAnimals.Remove(5);
            MyAnimals.RemoveAt(3);
            MyAnimals.RemoveRange(1, 2);
            MyAnimals.Remove(5);

            foreach (Animal animal in MyAnimals)
            {
                MessageBox.Show("You still have a " + animal.AnimalName + " (" + animal.AnimalType + ")", "CS559 - Assignment 2", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        // This will be called whenever the list changes.
        private static void ListChanged(object sender, AnimalListEventArgs e)
        {
            MessageBox.Show(e.Animal.AnimalName +" was removed from the list.", e.Animal.AnimalType+" Removed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s