471,317 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 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 1188
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Devin Fensterheim | last post: by
reply views Thread by Don | last post: by
1 post views Thread by Matt Gabbard | last post: by
3 posts views Thread by andreas.baus | last post: by

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.