Reusing Delegate objects

T

Tedb

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
 
W

Wessel Troost

Is there any reason why you can't reuse a delegate object instance versus
creating a new one each time?

A delegate contains a reference to an object and a reference to a
function. If neither changes, you can reuse the same delegate.

Your object is DataPoint, and the function is DataPoint_Changed. So I
think in your case it's safe te reuse the same delegate.

Confusingly, your "DataPoint" class contains an array of 64 "datapoint"s.
Now if you were to create a delegate to call a "datapoint" member
function, the object would differ for each delegate. In which case you'd
have to create a new delegate each time.

Greetings,
Wessel
 
T

Tedb

Thanks for the reply, if I understand you correctly it is proper to create
one delegate instance that points to a function of my client instance and
use it to subscribe to multiple datapoint changed events (saving a little
bit of overhead of creating multiple instances of delegate). What I am
trying to accomplish is that when any of the 64 datapoint instances fires
it's changed event, my client instance receives notification of this.
 
W

Wessel Troost

What I am
trying to accomplish is that when any of the 64 datapoint instances fires
it's changed event, my client instance receives notification of this.
That will work using the single delegate.

If efficiency is really an issue, you might want to work around having
arrays of objects fire events. You could define the change function in
the DataPoints object, like:

class DataPoints
{
public ChangeDataPoint( int iDatapointNumber, int iNewValue );
}

When the ChangeDataPoint function is called, the DataPoints class knows a
change is about to happen. In that way you are avoiding the overhead of
64+ events.

Greetings,
Wessel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top