I just thought of something after I wrote my last post...
_Options.Changed += ProductOptions_OnChange;
for (int i=0; i < _Options.Count; i++)
{
_Options[i].Values.Changed += ProductOptions_OnChange;
}
private void ProductOptions_OnChange(object sender, EventArgs
e)
{
_Options.HasChanged = true;
}
With this code in mind, you can see that the same event handler
(ProductOptions_OnChange) is triggered when either the Options collection is
modified OR any of the Options.Values data is modified and flips the boolean
value of Options.OnChange to true.
So Options.HasChanged == true whenever ANY data within the collection or any
of it's sub-collections is modified. This means I only have to do a check:
if (Options.HasChanged) { //do stuff }
If I set a private boolean flag within each custom collection, when it comes
time to check for the state, I'd have to do this:
if (Options.HasChanged || someFunction(Options.Values)) { //do stuff }
private bool someFunction(ProductOptionValues POV)
{
// loop through the ProductOptionValues collection and check the
HasChanged value on all items in the collection returning true if ANY of
them are true
}
Would this change your mind on whether events are the best approach to use?
Since the ProductOptionValues collection has no intrinsic knowledge of the
Options class, I can't set that flag from within it and am forced to use a
more complicated check of the value.
"Peter Duniho" <(E-Mail Removed)> wrote in message
news

(E-Mail Removed)...
> On Sat, 18 Oct 2008 12:14:57 -0700, Scott Stark <(E-Mail Removed)>
> wrote:
>
>> [...]
>> When I create an instance of the class, the Options collection gets
>> populated in the constructor. What I need to know is, has the options
>> collection (or its Values sub-collection) changed since the object was
>> instantiated?
>>
>> Since I'm adding products during initialization, I don't want the
>> boolean "HasChanged" value to be set there, so I'm wiring up the event
>> handlers after the collections are populated so I can detect any changes
>> post-initializaton.
>
> I would advise one of two alternatives, both of which involving having the
> "changed" state updated exactly where the collections are modified rather
> than relying on events:
>
> -- simply clear the state after initialization in the constructor
> -- suppress changing of the state by including some private flag in
> the class set during initialization
>
> I think the first approach is the simplest, but if you had some specific
> reason for not even wanting the state to change during initialization, the
> second might be appropriate.
>
> The events seem like overkill to me.
>
> Pete