Custom Event on Business Object

Custom Event on Business Object

Old forum URL: forums.lhotka.net/forums/t/797.aspx


jdelano posted on Thursday, August 03, 2006

Okay, I really hope all these hard drive crashes aren't because of me.  It seems like my posts are always the ones that get lost when something like that happens... :-(  how sad.

Anyway, I'd like to re-post the question and "half-answer" I put up yesterday morning, partially because it may help someone else down the line, and partially because I still have some unresolved questions.  Here goes...

I added a custom event to my business object following the pattern that Rocky covers in the book on page 101-102.  So far so good. I create a form with a private "datasource" object (based on the one with the custom event) and declare it as "WithEvents". I set the private data source to the parameter that I pass in to the form's constructor, and then call BeginEdit on the object (almost exact replica of the way the ProjectEdit form works in the ProjectTracker App).  In my form, I also have an event handler for the custom event in the business object. Code compiles fine, but when the form is actually shown, I get a "Form is not declared as serializable" (or some variant thereof) error. If I take away the event handler in the form, I get no errors--form displays fine. If I pop in an event handler for the PropertyChanged event, I get no errors either.

Rocky had suggested that I trace through the thing with the debugger, which I did. What I found was that at the call of BeginEdit, it was for some reason seeing the "mNonSerializableHandlers" as an available field to clone. As soon as I put the <NotUndoable()> attribute in front of it, then things worked fine. What I still don't understand is why the PropertyChanged custom event works fine WITHOUT the NotUndoable attribute, but my custom event seems to require it. Any ideas?

John Delano

xal replied on Thursday, August 03, 2006

I don't see why you would want the event to have serializable handlers if what you're attempting to do is simply have an event for "progress", then you're likely to only use that in the ui. So I'd go for NOT having serializable handlers, which will make your code more readable...


The reason for rocky's PropertyChanged event not having issues with nlevel undo is this:

PropertyChanged is declared in BindableBase.
UndoableBase Inherits from BindableBase.
When UndoableBase looks for things to undo, it goes through the inheritace hierarchy from your bo's type all the way up until UndoableBase. The reason that PropertyChanged doesn't fail, is that UndoableBase never goes looking for stuff to serialize in BindableBase.

So, in your place I would only have one handler that is marked as NotUndoable and NonSerialized.

Andrés

jdelano replied on Thursday, August 03, 2006

Aha!  That makes a ton of sense.  Perfect!

Thanks,

John Delano

Copyright (c) Marimer LLC