Hi all,
I have a custom collection that I am adding a ValidateItem event to. This ValidateItem event will be called whenever an item is added to or updated in the custom collection.
I want to allow derived classes to be able to subscribe to the event and determine their own logic for whether or not an item is "valid" and potentially prohibit it being added to the collection if it is "invalid".
But I am trying to figure out how to let the caller to the event know what is going on and how to pass the information about what is going on back.
My custom eventargs inherit from CancelEventArgs so I have the ability to pass the Cancel bit back to the caller using that. But I have never seen any cases where error information (error codes, messages, etc) gets passed back in this manner, so I'm wondering if this might not be the best approach.
Should I just add any error data that I wish to pass back to my Custom eventargs class, are there good reasons for or against this? or are there other, better ways to accomplish this?
Here is my eventargs class:
public delegate void ItemValidationEventHandler(object sender, ItemValidationEventArgs e);
public class ItemValidationEventArgs : CancelEventArgs
{
public ItemValidationEventArgs(object item, ObjectAction state, EventArgs e)
{
Item = item;
State = state;
EventArgs = e;
}
public ItemValidationEventArgs(object item, ObjectAction state) : this(item, state, new EventArgs())
{
}
public ItemValidationEventArgs() : this(null, ObjectAction.None, new EventArgs())
{
}
// is there a better way to pass this info?
public string ErrorMessage {get; set;}
public int ErrorNumber {get;set;}
public object Item { get; private set; }
public ObjectAction State { get; private set; }
public EventArgs EventArgs { get; private set; }
}
UPDATE: I suppose another other option would be to use something like this:
virtual bool Validate(object item, ObjectAction action, out string errorMessage)
method in the derived classes. Though I tend to prefer avoiding out parameters...
If anyone has any ideas on the pros and cons of each approach I'd love to hear em!
Thanks, Max