473,237 Members | 1,390 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,237 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 1275
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...

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.