469,133 Members | 1,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Reusing Delegate objects

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
Nov 17 '05 #1
4 2868
> 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
Nov 17 '05 #2
> 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
Nov 17 '05 #3
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.
"Wessel Troost" <no*****@like.the.sun> wrote in message
news:op.sudke8kpf3yrl7@asbel...
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

Nov 17 '05 #4
> 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
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Klaus Johannes Rusch | last post: by
3 posts views Thread by Phill | last post: by
4 posts views Thread by KC Eric | last post: by
reply views Thread by Tedb | last post: by
reply views Thread by Wavemaker | last post: by
1 post views Thread by Quimbly | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.