473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C# - list.Count property - how to catch?

JamieHowarth0
533 Expert 512MB
Hi guys,

Is there a way of either altering a list class, or creating some kind of listener, that throws an event when the Count property of a list changes?

Cheers,

codegecko
Feb 4 '09 #1
3 4658
IanWright
179 100+
How about something like this? Obviously you'll need to cover all the changes that can adjust the number of items contained within the list.

Expand|Select|Wrap|Line Numbers
  1. public class ListCountChang<T> : List<T>
  2. {
  3.     public event EventHandler CountChangeEvent;
  4.  
  5.     public void Add(T item)
  6.     {
  7.         base.Add(item);
  8.         CountChange();
  9.     }
  10.  
  11.    public void AddRange(IEnumerable<T> items)
  12.    {
  13.         base.AddRange(items);
  14.         CountChange();
  15.    }
  16.  
  17.     private void CountChange()
  18.     {
  19.          if(CountChangeEvent != null)
  20.          {
  21.              CountChangeEvent(this, EventArgs.Empty);
  22.          }
  23.     }
  24. }
  25.  
  26.  
Feb 4 '09 #2
vekipeki
229 Expert 100+
You will probably have to override System.Collections.ObjectModel.Collection<T>, since I think List<T> methods are not virtual.
Feb 5 '09 #3
IanWright
179 100+
vekipeki,

That sounds like a better idea. I tried something slightly different using the List.Count property as I originally misread the post, and although the methods were not virtual on the List it did work.

The new operator may work for hiding, but then I'm not sure if you can still call the base if you do that....

I think Collection<T> is probably the way to go too :)
Feb 5 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Majed | last post by:
hi all i've created a strong named collection which inherits collection base,but when i try to add to it a nullreferenceexception blows. the code is as listed below. do i have to init the list...
0
by: Steven | last post by:
Hi, I'm creating a custom checkbox list control which will take 2 arraylists as input. (One will contain Names and other will contain 0s and 1s. 0 - uncheck 1- check). I'm able to create the...
3
by: jason | last post by:
Hello. I've got this simple collection populate code I downloaded from the net (sorry can't find source now) I'm trying to test, but I can't seem to get it to work. Any help would be greatly...
8
by: Yuk Tang | last post by:
I am tearing my hair out over this, since I can't see what I'm doing wrong (duh, if I knew, I wouldn't be asking the question). I am adding Field items to a Field Collection, but for some reason...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
4
Rabbit
by: Rabbit | last post by:
Cascading Combo/List Boxes This tutorial is to guide you in the creation of Cascading combo/list boxes. That is when you have multiple combo/list boxes where the selection of an option in one...
7
by: Steve K | last post by:
First problem: I am specifying a format string for a Binding object like so: <code> Binding binding = new Binding("Text", item.EOBRemittance, "AmountAllowed", true, DataSourceUpdateMode.Never,...
6
by: Monty | last post by:
Hello, I have a singleton settings class (.Net 2.0 framework) that I serialize/deserialize to XML. On my settings class is a shared list of integers. If I have two numbers in my list and I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.