Re: Determine if an item in a collection has changed



Dmitriy-

Thanks for the response.

Let's say that i have two classes: Persons and Person.

The Persons class is a Collection class. It has two relevant methods: Add
and Save. The Add requires a Person reference. The Save persists all
objects in the collection that have changed (either by a batch update/insert
or calling each Person instances Save method).

The Person class is a custom object. It has two relevant, Friend members:
IsDirty property and Save method. If I make a change to the Person or
create a new person, IsDirty is true. The Save method manages the
insert/update.

I could look at the state of the Person instance in the Add method and set
internal state accordingly. However, I'm more concerned about changes to an
item already in the collection. For instance:

....
Dim PP as New Persons
PP.Load 'load from db

Dim P as Person: Set P = PP("#" & 10)
With P
.FirstName="Craig"
...
End With
Set P = Nothing

Set P = New Person
With P
...
End With

PP.Add P
Set P = nothing

PP.Save

....

Obviously, in PP.Save, i could enumerate each Person instance and call the
..Save method if the instance needed persistance, but i'd rather be smart
about it.

As i'm not creating an explicit WithEvents reference for each Person in the
Persons class, raising an event is out. I guess that leaves me with
classbacks.

Am I understanding the situation correctly?

Craig

"Dmitriy Antonov" <antonovdima@xxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:eALBbp05FHA.4036@xxxxxxxxxxxxxxxxxxxxxxx
>
> "Craig Buchanan" <someone@xxxxxxxxxxxxx> wrote in message
> news:%23dZ2o0z5FHA.3188@xxxxxxxxxxxxxxxxxxxxxxx
>> It's there an easy way to determine if one or more objects in a
>> collection has changed, without enumerating the collection and checking
>> each object individually? Is this a case for call backs?
>>
>> Thanks for the help.
>>
>
> If you mean an object of type Collection then it depends, what kind of
> items it contains. There is no way to notify about such changes if it
> contains items of primitive datatypes (yes you still can enumerate them
> and compare with some previous state as long as you preserve that state in
> some way). If it contains Objects created from classes, where such
> functionality is implemented than you can use that functionality. If you
> are talking about objects, which you create (or going to create) then you
> have many different options of doing this. You can add some property, like
> IsDirty or Changed, into your class and/or add event, which will be raised
> from the object of that class, when its state is changed.
>
> Callbacks? Well, you can use callbacks, if you know why. And yes you can
> use callbacks to notify other objects about changes in the state within
> items of your collection. Whether you need to do it depends on specifics
> of your application. Callback is just a mechanism, where some object holds
> a reference to another object and calls predefined method(s) of the latter
> one under certain conditions.
>
> Dmitriy.
>


.


Loading