473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 DataPointChange dEvent(DataPoin t aPoint);
public event DataPointChange dEvent Changed;
}

DataPoint[64] datapoint;

}

//This client creates a new delegate instance for each datapoint
class Client1
{

public void InitClient(Data Points dp)
{

for (int x=0; x< dp.datapoint.Le ngth;x++)
{
dp.datapoint[x].Changed += new
DataPoint.DataP ointChangedEven t(DataPoint_Cha nged);
}
}

void DataPoint_Chang ed(DataPoint aPoint)
{

}

}

//This client shares one delegate instance for all datapoint events
class Client2
{

public void InitClient(Data Points dp)
{

DataPoint.DataP ointChangedEven t DPChangedDelega te;

DPChangedDelega te = new
DataPoint.DataP ointChangedEven t(DataPoint_Cha nged);

for (int x=0; x< dp.datapoint.Le ngth;x++)
{
dp.datapoint[x].Changed += DPChangedDelega te;
}
}

void DataPoint_Chang ed(DataPoint aPoint)
{

}

}


Thanks for input.

Ted
Nov 17 '05 #1
4 3142
> 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_Chang ed. 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_Chang ed. 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.t he.sun> wrote in message
news:op.sudke8k pf3yrl7@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_Chang ed. 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 iDatapointNumbe r, 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
1982
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(); somename.show() Note that the class name "somename" is reused for the variable name.
3
8880
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 implement a sorting interface? I read that delegates are smaller and of finer granularity than interfaces. What does that mean exactly? Does it mean they are more efficient?
1
1143
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 the delegate by doing a 'new' whereas other objects just need to use += to add to the delegate list. Is there a way of doing this without each class testing whether to create the delegate first? class ClassX {
3
2325
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. Here's what I'm trying to do... I am iterating through the invocation list invoking each delegate manually. I want to eventually try to invoke a delegate to an object that has been destroyed. I'm essentially looking to get the exception...
4
2425
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
305
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 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...
0
2497
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 different threads besides itself. There is a concern for thread safety. There are various ways of dealing with this. One way is to make the class thread safe by putting in locks. But another way is to stream the events from the various threads into a...
0
850
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 and many dozens of instances of other objects (e.g. 100+ ClassA's, 10+ ClassB's and 50+ ClassC's etc). At runtime though, some objects will be configured to make callbacks to other objects, but this is defined at RUNTIME though. I want to be able...
1
1818
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 code, as there's simply too much, but here's the general gist: I have a connection object which connects to a custom back-end server of one type or another. Clients of this connection object send requests via a method (e.g....
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.