tags:

views:

58

answers:

3

Hi guys.

My problem is like this:

In ASP DetailsView component we have three different EventArgs for different DB operations: DetailsViewInsertedEventArgs, DetailsViewDeletedEventArgs, DetailsViewUpdatedEventArgs.

All above EventArgs have common properites, I am interested in two of them: Exception and ExceptionHandled. Unfortunately those two properties are not existant in the common ancestor for those event args.

I would like to create a method like this:

public void DoSomething(ref CommonAncestorForDVArgs args)
{

    if (args.Exception != null)
    {
        //do something with an exception
        args.ExceptionHandled = true;
    }
}

Of course this is not possible due to the fact that I've described earlier. Solution which I came up with is this:

public void DoSomething(Exception e, bool ExceptionHandled)
{

    if (e.Exception != null)
    {
        //do something with an exception
        ExceptionHandled = true;
    }
}

But I am wonder if there is something better?

A: 

You could use overloading to do what you want:

public void DoSomething (ref DetailsViewInsertedEventArgs args)
{
  DoCommonStuff (args);
  // do something with a DetailsViewInsertedEventArgs exception
}

public void DoSomething (ref DetailsViewDeletedEventArgs args)
{
  DoCommonStuff (args);
  // something with a DetailsViewDeletedEventArgs exception
}

public void DoSomething (ref DetailsViewUpdatedEventArgs args)
{
  DoCommonStuff (args);
  // do something with a DetailsViewUpdatedEventArgs exception
}

void DoCommonStuff (ref CommonAncestorForDVArgs args)
{
  // common stuff
}
Skizz
Thanks, but there will be maintenance nightmare later...
Wodzu
+1  A: 

You can use an interface and derive the EventArgs from the interface and EventArgs (or whatever derived EventArgs you are using), like:

public interface ICommonAncestorForDVArgs
{
    Exception Exception { get; set; }
    bool ExceptionHandled { get; set; }
}

and then using this interface in your DoSomething method by:

public void DoSomething(ref ICommonAncestorForDVArgs args)

EDIT: Another way of doing this will be Reflection. You can program your DoSomething method like this (this code does not include error checking):

public static void DoSomething<T>(ref T args)
{
    Exception e = args.GetType().GetProperty("Exception").GetValue(args, null) as Exception;

    if (e != null)
    {
        //do something with an exception

        typeof(CommonAncestorForDVArgs).GetProperty("ExceptionHandled").SetValue(args, true, null);
    }
}
Yogesh
That would be cool, but DetailsViewInsertedEventArgs, DetailsViewDeletedEventArgs, DetailsViewUpdatedEventArgs are not defined by me, they are part of .ASP framework. I can not redefine them to use this interface.
Wodzu
Oops. I didn't checked that. The only other way to do that then will be reflection I guess.
Yogesh
Added the new reflection version.
Yogesh
A: 

Alternatively, use Reflection:

public void DoSomething (ref CommonAncestorForDVArgs args)
{
  if (args.GetType () == typeof DetailsViewInsertedEventArgs || // syntax probably wrong here
      args.GetType () == some.other.type)
  {
    // do something with an exception
  }
}

In terms of maintenance, it's probably not that much different from my other answer.

Skizz
Yes, that is what I need, thanks.
Wodzu