Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:05:06+00:00 2026-06-12T18:05:06+00:00

My goal is to extend the Collection object to support to flag when an

  • 0

My goal is to extend the Collection object to support to flag when an item is added, updated or removed from the Collection. The MSDN article provides an example (Example 2) of extending for a specific type. I would like to keep it Generic so I don’t have to implemnent a new Class for every Collection I have.

Here is what I have so far:

public class ChangedEventArgs<T> : EventArgs
{
    public readonly T ChangedItem;
    public readonly T ReplacedWith;
    public readonly ChangeType ChangeType;

    public ChangedEventArgs(ChangeType change, T item, T replacement)
    {
        ChangeType = change;
        ChangedItem = item;
        ReplacedWith = replacement;
    }
}

public enum ChangeType
{
    Added,
    Removed,
    Replaced,
    Cleared
};

class CollectionChangeTracked<T> : Collection<T>
{
    public event EventHandler<ChangedEventArgs<T>> Changed;

    protected override void InsertItem<TParam>(int index, TParam newItem)
    {
        base.InsertItem(index, newItem);

        EventHandler<ChangedEventArgs<T>> temp = Changed;
        if (temp != null)
        {
            temp(this, new ChangedEventArgs<T>(ChangeType.Added, newItem, null));
        }
    }

    protected override void SetItem<TParam>(int index, TParam newItem)
    {
        string replaced = Items[index];
        base.SetItem(index, newItem);

        EventHandler<ChangedEventArgs<T>> temp = Changed;
        if (temp != null)
        {
            temp(this, new ChangedEventArgs(ChangeType.Replaced, replaced, newItem));
        }
    }

    protected override void RemoveItem<TParam>(int index)
    {
        TParam removedItem = Items[index];
        base.RemoveItem(index);

        EventHandler<ChangedEventArgs<T>> temp = Changed;
        if (temp != null)
        {
            temp(this, new ChangedEventArgs<T>(ChangeType.Removed, removedItem, null));
        }
    }

    protected override void ClearItems()
    {
        base.ClearItems();

        EventHandler<ChangedEventArgs<T>> temp = Changed;
        if (temp != null)
        {
            temp(this, new ChangedEventArgs(ChangeType.Cleared, null, null));
        }
    }
}

Question:
I’m receiving an error on the line: base.InsertItem(index, newItem)
It says I have invalid arguments but nothing more. Same issue in SetItem and RemoveItem.

Updated code that compiles:

public class ChangedEventArgs<T> : EventArgs
{
    public readonly T ChangedItem;
    public readonly T? ReplacedWith;
    public readonly ChangeType ChangeType;

    public ChangedEventArgs(ChangeType change, T item, T? replacement)
    {
        ChangeType = change;
        ChangedItem = item;
        ReplacedWith = replacement;
    }
}

public enum ChangeType
{
    Added,
    Removed,
    Replaced,
    Cleared
};

class CollectionChangeTracked<T> : Collection<T>
{
    public event EventHandler<ChangedEventArgs<T>> Changed;

    protected override void InsertItem(int index, T newItem)
    {
        base.InsertItem(index, newItem);

        EventHandler<ChangedEventArgs<T>> temp = Changed;

        if (temp != null)
            temp(this, new ChangedEventArgs<T>(ChangeType.Added, newItem, default(T)));
    }


    protected override void SetItem(int index, T newItem)
    {
        T replaced = Items[index];
        base.SetItem(index, newItem);

        EventHandler<ChangedEventArgs<T>> temp = Changed;

        if (temp != null)
            temp(this, new ChangedEventArgs<T>(ChangeType.Replaced, replaced, newItem));
    }

    protected override void RemoveItem(int index)
    {
        T removedItem = Items[index];
        base.RemoveItem(index);

        EventHandler<ChangedEventArgs<T>> temp = Changed;

        if (temp != null)
            temp(this, new ChangedEventArgs<T>(ChangeType.Removed, removedItem, null));
    }

    protected override void ClearItems()
    {
        base.ClearItems();

        EventHandler<ChangedEventArgs<T>> temp = Changed;

        if (temp != null)
            temp(this, new ChangedEventArgs<T>(ChangeType.Cleared, null, null));
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:05:07+00:00Added an answer on June 12, 2026 at 6:05 pm

    @daveL is totally right, what you want is a generic event args:

    public class ChangedEventArgs<T> : EventArgs
    {
        public readonly T ChangedItem;
        public readonly T ReplacedWith;
        public readonly ChangeType ChangeType;
    
        public ChangedEventArgs(ChangeType change, T item, T replacement)
        {
            ChangeType = change;
            ChangedItem = item;
            ReplacedWith = replacement;
        }
    }
    

    But what he has missed in his answer is how to use it; first make your event handler use the same type T as the main class:

    class CollectionChangeTracked<T> : Collection<T>
    {
        public event EventHandler<ChangedEventArgs<T>> Changed;
    
       ....
    
    }
    

    Then, your individual methods, rather than redefining the type as TParam should be using the T defined at class level:

    protected override void InsertItem(int index, T newItem)
    {
        base.InsertItem(index, newItem);
        EventHandler<ChangedEventArgs<T>> temp = Changed;
        if (temp != null)
        {
          // Added <T> after ChangedEventArgs  
          temp(this, new ChangedEventArgs<T>(ChangeType.Added, newItem, default(T)));
        }
    }
    

    Live example: http://rextester.com/XFCLN26271

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to extend the already set style of an object. So assuming
My goal is to extend functionality of the current asset tracking system . Basically
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal
Goal: Produce an Excel document with information from 3 associated models that is similar
Ultimately, my goal is to extend Django's ModelAdmin to provide field-level permissions—that is, given
The goal I need to achieve is to get all records from a cursor
I am in need of creating a dynamically extend-able class in C#. The goal
My goal is to create a trait that a case class can extend, which
Goal : I wants when I drag image it become fade so we can
Goal is to make a dialog that appears on menu_key pressed, but it keeps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.