473,325 Members | 2,785 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,325 software developers and data experts.

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<Permission2Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission>();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permissions.Add(newPermission). 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 1282
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<Permission2Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission>();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permissions.Add(newPermission). 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...@hotmail.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******@hotmail.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<Tinstead 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**********@gmail.com
Blog: http://devlicio.us/blogs/marcin_hoppe
Jun 27 '08 #6
On May 7, 7:12*pm, daokfella <jjbut...@hotmail.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<Permission2Permissions
{
* *get
* *{
* * * if (_Permissions == null)
* * * {
* * * * *_Permissions = new List<Permission>();
* * * }
* * * return _Permissions;
* *}

}

The consumer of this can now say
myobject.Permissions.Add(newPermission). 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.Collections.ObjectModel.ObservableCollectio n<T>, it
might be what you need.

"daokfella" <jj******@hotmail.comwrote in message
news:e0**********************************@a23g2000 hsc.googlegroups.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<Permission2Permissions
{
get
{
if (_Permissions == null)
{
_Permissions = new List<Permission>();
}
return _Permissions;
}
}

The consumer of this can now say
myobject.Permissions.Add(newPermission). 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
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...
2
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 =...
0
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...
5
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...
1
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...
3
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...
2
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,...
2
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...
8
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.