pdavila (10) [Avatar] Offline
#1
Erik,

In your implementation of the album class in chapter 5, you override the OnClear method instead of the Clear method.

Could you explain when to do either or both? That is, why did you prefer to override the OnClear instead of doing the same work on the Clear?

Thanks,

Peter
eebrown (89) [Avatar] Offline
#2
Re: Collection Base - OnClear vs. Clear (Chapter 5)
Sure. The only reason to override Clear() is if we wanted to do something entirely different than what the CollectionBase.Clear() method already does for us. In our case, what Clear() does is just fine, we just want to add to it.

Since we derive PhotoAlbum from CollectionBase, we can access the protected members. The protected OnClear() method is provided to perform additional work during a Clear(), which is exactly what we want to do in chapter 5.

As you've realized, you could make this work either way, but the OnClear() seems more appropriate, and is in fact invoked by CollectionBase whenever Clear() is called.

Erik
pdavila (10) [Avatar] Offline
#3
Re: Collection Base - OnClear vs. Clear (Chapter 5)
Erik,

Thanks for the response.

Your explanation is the best description of the differences between the "OnX" and "X" methods that I have seen anywhere.

Once again, thanks for the great book. I look forward to reading the rest of it.

Thanks,

Peter