473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3121
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
3
by: Phill | last post by:
When should you use an interface over a delegate? Like if you have a class that needs to sort various types of objects. Should it use a delegate to do the sorting or require that the objects...
1
by: Darren | last post by:
If I have a class that exposes a delegate (see following code), how do other objects who wish to receive notifications be coded? What I mean is the first object to receive notifications must create...
3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
4
by: KC Eric | last post by:
Hi all, I have a dll file, it has a class, say: class Temp, this class has a function which has a delegate as a parameter, say: public void Test(GameOverHandler _overHandler)
0
by: Tedb | last post by:
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...
0
by: Wavemaker | last post by:
One of the things I've struggled with from time to time is handling events raised on different threads. For example, an object could be listening to events from one or more objects running in...
0
by: Nate | last post by:
I am creating an application that is quite complex. There will be several dozen objects, most of which have delegates defined. At runtime, there will be several hundred instances of some objects...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.