19 Oct, 2008, Vassi wrote in the 1st comment:
Votes: 0
So I was re-factoring my network stack (again) to enforce\provide a bit more functionality and I ran into something that I always have to stop and think about with events. This really is more a matter of style, or taste, so I was curious what you guys think about the number of events exposed by a class. In this case I'm working on Telnet, so I could have the following events:

public enum ChangeType{ Enabled, Disabled }

public class OptionChangeArgs : EventArgs
{
public byte Option { get; set; }
public bool Handled { get; set; }
public ChangeType Status { get; set;}

public OptionEnabledArgs(byte opt, OptionChange status)
{
Option = opt;
Status = status;
}
}



public event EventHandler<SubNegotiationArgs> SubNegotiationReceived;
public event EventHandler<OptionChangeArgs > LocalOptionChanged;
public event EventHandler<OptionChangeArgs > RemoteOptionChanged;


This style lets you lower the number of events, but makes your event handlers more complex by having to check the change type. It also introduces an extra enum for specifying the kind of change. Alternatively, I could have:

public class OptionChangedArgs : EventArgs
{
public byte Option { get; set; }
public bool Handled { get; set; }

public OptionChangedArgs(byte opt)
{
Option = opt;
}
}

….
public event EventHandler<SubNegotiationArgs> SubNegotiationReceived;
public event EventHandler<OptionChangedArgs> LocalOptionEnabled;
public event EventHandler<OptionChangedArgs> RemoteOptionEnabled;
public event EventHandler<OptionChangedArgs> LocalOptionDisabled;
public event EventHandler<OptionChangedArgs> RemoteOptionDisabled;


This option requires twice as many events, and consequently event handlers, but it may be clearer to understand and the event handlers can be a bit less complex. What do you guys prefer? I think I lean towards the second implementation, and I think that's what I'll be implementing when I click Create Topic, but I'm curious what works for everyone else.
19 Oct, 2008, David Haley wrote in the 2nd comment:
Votes: 0
If the event is the change of a binary condition, I'd probably go for a version with the status. But I definitely wouldn't make an enum if the only values are true and false. I don't view having to check the type as such a big problem, and it is nice to uniformly handle certain events. For example, anytime a given flag is switched, you might want to do something in particular. If you have one handler, it's easier to maintain that common behavior. If you have two handlers, you have a little more work to do.
0.0/2