S
Scott Stark
Hello,
I've got some code working but I'm not sure that the way I implemented it is
the best way. I have a custom collection class with a boolean property
HasChanged that tells me if any of the data has changed. An event is raised
on certain methods (Add, Remove, Insert, etc). Let me write a little code to
describe what I've done and see if there's room for improvement:
I want to represent a class that functions like this when instantiated.
Product P = new Product(productID);
P.Options.Name = "Gender";
P.Options.Values[0] = "Male";
P.Options.Values[1] = "Female;
I need to set events on both the custom "Options" collection as well as the
custom "Values" collection contained within Options so that I know if either
a new option has been added or if a value has been modified.
Here's my solution (which again, work just fine, looking for areas I can
improve on):
<< THERE ARE ONE OF THESE FOR ProductOptionsCollection and
ProductOptionValuesCollection. ONLY DISPLAYING ONE FOR BREVITY >>
namespace Sample
{
public delegate void ProductOptionChangedEventHandler(object sender,
EventArgs e);
public class ProductOptionCollection : CollectionBase
{
public event ProductOptionChangedEventHandler Changed;
private bool _HasChanged;
public bool HasChanged
{
get { return _HasChanged; }
set { _HasChanged = value; }
}
public int Add(ProductOption item)
{
OnChanged(new EventArgs());
return List.Add(item);
}
.. OTHERS REMOVED FOR BREVITY..
protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this, e);
}
}
}
Then, within the calling class:
_Options.Changed += ProductOptions_OnChange;
IS THIS THE RIGHT WAY TO REGISTER FOR EVENTS WITHN A COLLECTION
OF A COLLECTION?
for (int i=0; i < _Options.Count; i++)
{
_Options.Values.Changed += ProductOptions_OnChange;
}
NOTICE I'M SETTING A BOOLEAN VALUE WITHIN THE CUSTOM COLLECTION
THAT RAISED THE EVENT IN THE FIRST PLACE -- I can't just set it within the
collection itself when Add is called because I need to programatically
register the event handler after the initial population of the collection.
Is there a better way to do this?
private void ProductOptions_OnChange(object sender, EventArgs e)
{
_Options.HasChanged = true;
}
I've got some code working but I'm not sure that the way I implemented it is
the best way. I have a custom collection class with a boolean property
HasChanged that tells me if any of the data has changed. An event is raised
on certain methods (Add, Remove, Insert, etc). Let me write a little code to
describe what I've done and see if there's room for improvement:
I want to represent a class that functions like this when instantiated.
Product P = new Product(productID);
P.Options.Name = "Gender";
P.Options.Values[0] = "Male";
P.Options.Values[1] = "Female;
I need to set events on both the custom "Options" collection as well as the
custom "Values" collection contained within Options so that I know if either
a new option has been added or if a value has been modified.
Here's my solution (which again, work just fine, looking for areas I can
improve on):
<< THERE ARE ONE OF THESE FOR ProductOptionsCollection and
ProductOptionValuesCollection. ONLY DISPLAYING ONE FOR BREVITY >>
namespace Sample
{
public delegate void ProductOptionChangedEventHandler(object sender,
EventArgs e);
public class ProductOptionCollection : CollectionBase
{
public event ProductOptionChangedEventHandler Changed;
private bool _HasChanged;
public bool HasChanged
{
get { return _HasChanged; }
set { _HasChanged = value; }
}
public int Add(ProductOption item)
{
OnChanged(new EventArgs());
return List.Add(item);
}
.. OTHERS REMOVED FOR BREVITY..
protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this, e);
}
}
}
Then, within the calling class:
_Options.Changed += ProductOptions_OnChange;
IS THIS THE RIGHT WAY TO REGISTER FOR EVENTS WITHN A COLLECTION
OF A COLLECTION?
for (int i=0; i < _Options.Count; i++)
{
_Options.Values.Changed += ProductOptions_OnChange;
}
NOTICE I'M SETTING A BOOLEAN VALUE WITHIN THE CUSTOM COLLECTION
THAT RAISED THE EVENT IN THE FIRST PLACE -- I can't just set it within the
collection itself when Add is called because I need to programatically
register the event handler after the initial population of the collection.
Is there a better way to do this?
private void ProductOptions_OnChange(object sender, EventArgs e)
{
_Options.HasChanged = true;
}