CollectionBase.OnSetComplete

  • Thread starter Thread starter Shannon Richards
  • Start date Start date
S

Shannon Richards

Can anyone tell me what the CollectionBase.OnSetComplete() overridable
method is supposed to be used for?

MyCollectionClass(Index) = Object triggers this method but why does
"setting" in a collection class actually do? Reset the enumerator?

Sorry for the cross group posting.

Any insight would be greatly appreciated

Shannon Richards
BBA, AIT, MCP
 
Shannon Richards said:
Can anyone tell me what the CollectionBase.OnSetComplete() overridable method is supposed to be used for?

It's a protected sub that you can override in your CollectionBase
subclass, and it is called by the .NET Framework after anybody
assigns a new object to this collection. There's a companion sub,
OnSet( ), that gets called immediately before the new object joins
the collection. OnSetComplete( ) is called immediately after the
new object has joined the collection.

The arguments to OnSetComplete( ) let you examine the object
that used to be in your collection at the given index, as well as the
new object. Here are three possible scenarios that may require
special handling after an object in the collection has been set (i.e.,
not inserted, added, or removed, but merely a new object assigned
in-place within the collection).

1. You may decide in your handling of OnSetComplete( )
to reject the assignment, and replace the new object with the old
object (maybe the new object fails to meet some business rule
that your collection requires).

2. Perhaps you're tracking viewstate for the objects in your
collection (e.g., maybe your collection holds the buttons of a
toolbar control), and you can check the new object to see if
it's already tracking viewstate and if not, cause it to start
tracking viewstate.

3. Maybe everytime your collection changes, it needs to fire
an event that notifies another object in your application. For
example, your collection may hold e-mail messages from several
automated build servers across an enterprise. Anytime a new
e-mail arrives in the collection, or an administrator has logged
on and changed the status flags of an existing e-mail, this
collection needs to be able to add a record to the Event Log
or a database.

For more information (please excuse the line-wrap in this URL, it
should all be one-line),

http://msdn.microsoft.com/library/e...ionsCollectionBaseClassOnSetCompleteTopic.asp


Derek Harmon
 
Derek,
Your reply was in my mind the perfect reply to a question. It was not a
"this is how I would do it", or a "go google for your answer". It was a
eloquently crafted reply that shows both a knowledge of the easy answer to
the question and most importantly a path to the answer as to why and how you
would leverage this information with an application.

Hats off

Thanks
Lloyd Sheen
 
Hello: Thank you for your reply. I appreciate the detailed
explanation...very helpful!!! Below I have outlined a related issue I have
been trying to solve...Any insight would be greatly appreciated :-).

I currently have a situation where I have a Collection class derived from
CollectionBase. In this custom collection object I have implemented all the
required interfaces to support complex data binding.
I want child objects to notify the collection class whenever a change occurs
(example: Property value changed). I then want the collection object to
raise the "ListChanged" event defined in the IBindingList interface. This
event will notify any complex controls bound to my collection class that
they should refresh their display.

example (child object - Name property):
Public Property Name() As String
Get
Return ms_Name
End Get
Set(ByVal Value As String)
' This event supports simple data binding
RaiseEvent NameChanged(Me, New EventArgs)

' This causes the OnSetComplete() method of the collection class to
fire
Dim li_Index as Integer = mo_ParentList.IndexOf(Me)
mo_ParentList(li_Index) = Me
End Set
End Property

This code "sets" the current item in the collection to the now modified
child object...This triggers the OnSet() and OnSetComplete() methods of the
collection class. I am currently raising the "ListChanged" event in the
OnSetComplete() method of the collection class...

This notification works perfectly except when I change properties on
multiple objects inside a loop. I get an error because the Enumerator gets
messed up when the "set" occurs inside a loop.

example (Collection Object - Save method)
....
Dim lo_Object as Object

For Each lo_Object in MyCollectionObject
lo_Object.Property = "Some Value"
lo_Object.Save()
Next
....

Any thoughts on a better way to notify the parent object that a change has
occurred in a child object? At this point I think it would be better to
raise an event (example: a ListItemChanegd() event) from the child object
that the CollectionClass can listen for. Then the Collection class could in
turn raise the ListChanged event to facilitate the chain of notification...

Thank you again for the help!!!
Shannon Richards
BBA, AIT, MCP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top