uncle hammy

OK, I know an interface can't contain static members of any kind, so let me explain what I am trying to do, and see if anyone has any ideas how to do it...

I have several business objects in my app that all have a static "ObjectAction" event. This event gets fired when an object is added, edited, or deleted, and the eventargs contain the type of action that took place.

Now, I have also built a ListBox<T> which will make a BusinessObject (T) specific listbox.

The reason I have made the ObjectAction event static, is so that my listbox<T> can subscribe to it, and say add the business object to it's list when the event fires with e.Action == Added.

Right now, I wrap ObjectListBox<T> to make my final control (i.e. CompanyListBox : ObjectListBox<Company>), and I can subscribe to the static events in each wrapped control. But what I would really like to do is create a IBusinessObject interface that defines the ObjectAction event in it, then have each of my business objects implements this interface. This way, I can move my event subscription to the gereric list box, rather than doing it for each wrapped one.

Now that I have completely danced around that explanation....I want to end up with this...

public class BusinessObject : IBusinessObject
{
// IBusinessObject member
public static event BusinessObjectActionEventHandler Action;

blah, blah, blah
}

public class ObjectListBox<T> : ListBox where T : IBusinessObject
{
public ObjectListBox()
{
T.Action += new BusinessObjectActionEventHandler(EventHandler);
}
}

private EventHandler()
{
blah, blah
}

Does this make sense, and anyway to accomplish this

TIA


Re: Visual C# General Interface, Static Events??

Steve Py

How about using an Abstract class as your BusinessObject instead





Re: Visual C# General Interface, Static Events??

uncle hammy

Yes, I could. I was hoping for an interface way around it, because some of my business classes already inherit another class (i.e. Contact and Employee both inherit the abstract class Person).




Re: Visual C# General Interface, Static Events??

Peter Ritchie

uncle hammy wrote:
Yes, I could. I was hoping for an interface way around it, because some of my business classes already inherit another class (i.e. Contact and Employee both inherit the abstract class Person).
An interface approach to what you want to do is essentially impossible. A virtual table is used lookup the "inherited" interface members. Static classes don't have a virtual table and static methods aren't in a virtual table for instance classes. Yes, the framework could be changed to accommodate that; but it's not likely that that will happen.




Re: Visual C# General Interface, Static Events??

uncle hammy

So, then I guess my answer is to either remove the Person (for instance) inheritance, and create my BusinessBase abstract class. Or, code event hookups in each list box wrapper then




Re: Visual C# General Interface, Static Events??

sirjis

You could also try making Person inherit from BusinessBase, and then keep the other two classes as inheriting from Person.



Re: Visual C# General Interface, Static Events??

uncle hammy

Well DUH ....where was my head at

Though apparently this breaks down, and my head can't seem to tell me why...

public abstract class BusinessBase<T>
{
blah, blah
}

public abstract class Person : BusinessBase<Person>
{
blah, blah
}

public class Contact : Person
{
blah, blah
}

Now, if Person is a BusinessBase<T>, and Contact is a Person, then Contact should be a BusinessBase<Person>, correct

However, this causes a compile error that Contact cannot be converted to BusinessBase<Contact>, therefore T cannot be used.

public abstract class ObjectListBox<T> : ListBox where T : BusinessBase<T>
{

}

public class ContactListBox : ObjectListBox<Contact>
{

}