473,657 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I intercept when an item is added or removed from a List<ofType>?

I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?

private List<Permission _Permissions;

public List<Permission 2Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission >();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permis sions.Add(newPe rmission). I'd like to be able to run
some code when this happens. What's the best way?

Thanks,

Jason
Jun 27 '08 #1
7 1293
daokfella wrote:
I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?

private List<Permission _Permissions;

public List<Permission 2Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission >();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permis sions.Add(newPe rmission). I'd like to be able to run
some code when this happens. What's the best way?
The obvious solution would be not to expose the entire list
as a property but instead expose add and remove methods that
modified the list and did whatever you want them do.

Arne
Jun 27 '08 #2
Yes, I thought of that originally, but consumers of the object need to
have access to the list. I'm thinking of just creating a custom class
that inherits from IList that will raise events during add and remove.
Jun 27 '08 #3
On May 7, 10:03 pm, daokfella <jjbut...@hotma il.comwrote:
Yes, I thought of that originally, but consumers of the object need to
have access to the list. I'm thinking of just creating a custom class
that inherits from IList that will raise events during add and remove.
i think you can use the collection class..

http://dotnetcorner.weblog.com/2008/...ollection.html
Jun 27 '08 #4
On Wed, 07 May 2008 16:12:56 -0700, daokfella <jj******@hotma il.comwrote:
I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?
You can use BindingList<Tin stead of List<T>. That class has a
ListChanged event that notifies you of changes to the list.

Pete
Jun 27 '08 #5
daokfella pisze:
Yes, I thought of that originally, but consumers of the object need to
have access to the list.
Why exactly do they need the list for? If it is sequential access, you
can expose IEnumerable member. If it is some kind of index lookup, you
can expose proper method that takes an index and returns proper item.

Best regards!
--
Marcin Hoppe
Email: ma**********@gm ail.com
Blog: http://devlicio.us/blogs/marcin_hoppe
Jun 27 '08 #6
On May 7, 7:12*pm, daokfella <jjbut...@hotma il.comwrote:
I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?

private List<Permission _Permissions;

public List<Permission 2Permissions
{
* *get
* *{
* * * if (_Permissions == null)
* * * {
* * * * *_Permissions = new List<Permission >();
* * * }
* * * return _Permissions;
* *}

}

The consumer of this can now say
myobject.Permis sions.Add(newPe rmission). I'd like to be able to run
some code when this happens. What's the best way?

Thanks,

Jason
Hi,

List<Tdoes not expose this feature, as a matter of fact in code
analysis you have a role because of this.

Take a look at this post: http://blogs.msdn.com/fxcop/archive/...27/585476.aspx
Jun 27 '08 #7
Take look at System.Collecti ons.ObjectModel .ObservableColl ection<T>, it
might be what you need.

"daokfella" <jj******@hotma il.comwrote in message
news:e0******** *************** ***********@a23 g2000hsc.google groups.com...
I have a business object that exposes a collection of other objects
via a List<of Type>. How can I intercept when an item is either added
or removed from this list. Is it possible?

private List<Permission _Permissions;

public List<Permission 2Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission >();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permis sions.Add(newPe rmission). I'd like to be able to run
some code when this happens. What's the best way?

Thanks,

Jason
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3475
by: Devin Fensterheim | last post by:
Does anyone know of an issue with a control's viewstate not persisting when dynamically adding custom controls to a web form using AddAt? If a control is added to an HTML form directly using the HtmlForm's AddAt method (per the below code segment), then LoadViewState is never fired for the control, even if SaveViewState, which is fired, saves to the control's ViewState. However, if Add is used instead of AddAt, LoadViewState is called...
2
1440
by: Simon Prince | last post by:
Help I have a ASP:Listbox on a form. My page Adds items to this this via Client-Side Script only. Such as... var vObj_TargetElement = document.getElementById("_PageTemplate_innerHolder_oASP_Lbx_Invoice"); var vObj_SourceElement = document.getElementById("oHTML_Tbx_Invoice"); var vInt_OptionTotal = vObj_TargetElement.length++;
0
1009
by: Don | last post by:
I need some suggests on how to manage the viewState on my page. I have multiple checkBoxLists that are dynamically created and need then synced up with the viewstate. My problem is that on each postback I may have less items than the viewstate has because some are not created. When the viewState is loaded it seems to load the controls in the order they were created so if the first control is now gone it's data is assigned to what was...
5
1256
by: Gerry | last post by:
I need to populate a drop-down list box with names from a table (via SQL Stored Proc). Each name has a unique ID associated with them e.g. NameID 1 Name "My Name" The problem is how can I tell the combo box the ID associated with each name added to it.
1
3509
by: Matt Gabbard | last post by:
Can someone think of a way or an event that is fired when an item is added to a listview? I have a running total box which needs to stay updated every time a listviewitem is added or removed, but can not find an event fired on these occasions... Thanks, Matt
3
12894
by: andreas.baus | last post by:
Hello. Is there a way to add custom code that is executed every time a ListViewItem is added to the Items collection of a ListView control? There does not seem to be an event that I could hook into, and I can't figure out how derive a custom ListViewItemCollection to override it's Add() Method...
2
1108
by: sck10 | last post by:
Hello, I am using the following and am trying to figure out how to select the nth item of a list in a DropDownList. //Insert and Add items to the DropDownList ddlPayStatus.Items.Insert(0, new ListItem("Non Selected", "Non Selected"));
2
1913
by: RB | last post by:
Hi there, I'm having a problem with an ASP.NET/VB.NET Control I am writing. The control is a simple gallery control, which shows a set of thumbnails (using a DataList), and a main image of the selected thumbnail. It also has "Add Picture" and "Delete Picture" buttons. "Add Picture" will add a new picture, while "Delete Picture" will remove the currently selected picture. These are both ASP:ImageButtons, with the functionality...
8
2668
by: azegurb | last post by:
Hi i have taken news script with image upload feature it works fine. it show image and its being getting smalled thumbnail at the right side but when i added news without image instead of image it displays red instead of little (thumbnail) image X sign you can see script from here you can go here http://aziko.6te.net/yukle/haber_ekle.php and you can view the news you added from here http://aziko.6te.net/yukle/ here is full code ...
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.