Is there any reason why you can't reuse a delegate object instance versus
creating a new one each time? For example in the following scenario I have
a DataPoints object with an array of DataPoint objects, each of these has a
Changed event. My Client objects subscribe to some or all of the DataPoint
Changed events. Client1 creates a new delegate instance for each DataPoint
event subscribed to, while Client2 creates one delegate instance and shares
it for all DataPoint events. Here is an example (air code):
class DataPoints
{
class DataPoint
{
public delegate void DataPointChangedEvent(DataPoint aPoint);
public event DataPointChangedEvent Changed;
}
DataPoint[64] datapoint;
}
//This client creates a new delegate instance for each datapoint
class Client1
{
public void InitClient(DataPoints dp)
{
for (int x=0; x< dp.datapoint.Length;x++)
{
dp.datapoint[x].Changed += new
DataPoint.DataPointChangedEvent(DataPoint_Changed) ;
}
}
void DataPoint_Changed(DataPoint aPoint)
{
}
}
//This client shares one delegate instance for all datapoint events
class Client2
{
public void InitClient(DataPoints dp)
{
DataPoint.DataPointChangedEvent DPChangedDelegate;
DPChangedDelegate = new
DataPoint.DataPointChangedEvent(DataPoint_Changed) ;
for (int x=0; x< dp.datapoint.Length;x++)
{
dp.datapoint[x].Changed += DPChangedDelegate;
}
}
void DataPoint_Changed(DataPoint aPoint)
{
}
}
Thanks for input.
Ted