473,665 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instantiating objects and event handlers for delegates

S Name Microsoft Windows XP Professional
Version 5.1.2600 Service Pack 2 Build 2600
Total Physical Memory 1,024.00 MB

MDE 2003 Version 7.1.3008
..NET Framework 1.1 Version 1.1.4322 SP1
Microsoft Visual C# .NET 69462-335-0000007-18707
Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3

The code below shows the instantiation of multiple V47 objects. These
objects have several delegates that are fired by private V47 methods. These
private methods run on timers. In the code below, as well as instantiating
multilpe V47 objects I have instantiated an event handler
(V47.WTGDataRow Ready_10) for each V47 object. Apperarently, one handler for
each V47 object. IS THIS THE CORRECT WAY TO DO THIS?

foreach ( DataRow TagNameRow in DSTagNames.Tabl es["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in
DSWTG.Tables["TurbineConfigu ration"].Rows )
{
switch ( ConfigRow["Turbine Type"].ToString())
{
case "V47":
// instantiate a new turbine
VestasWTG V47 = new VestasWTG( TagNameRow, ConfigRow);
// There are several other delegates instantiated that are
not shown
// 10 min datarow delegate
V47.WTGDataRowR eady_10 += new
Trillium.WindSp ace.VestasWTG.W TGDataRowReady1 0Delegate(WTGDa taRowReady10Han dler); break;
default: break;
}
}
}
Nov 17 '05 #1
2 2267
If every instance of a V47 handles the event in exactly the same way and
there is no variation, I would suggest handling the event directly in the
class rather than exposing the event. Alternatively, handle the event in
the class and still expose the event and let the class consumers add more to
the process if they need to.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"FredC" <Fr***@discussi ons.microsoft.c om> wrote in message
news:CC******** *************** ***********@mic rosoft.com...
S Name Microsoft Windows XP Professional
Version 5.1.2600 Service Pack 2 Build 2600
Total Physical Memory 1,024.00 MB

MDE 2003 Version 7.1.3008
.NET Framework 1.1 Version 1.1.4322 SP1
Microsoft Visual C# .NET 69462-335-0000007-18707
Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3

The code below shows the instantiation of multiple V47 objects. These
objects have several delegates that are fired by private V47 methods. These private methods run on timers. In the code below, as well as instantiating
multilpe V47 objects I have instantiated an event handler
(V47.WTGDataRow Ready_10) for each V47 object. Apperarently, one handler for each V47 object. IS THIS THE CORRECT WAY TO DO THIS?

foreach ( DataRow TagNameRow in DSTagNames.Tabl es["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in
DSWTG.Tables["TurbineConfigu ration"].Rows )
{
switch ( ConfigRow["Turbine Type"].ToString())
{
case "V47":
// instantiate a new turbine
VestasWTG V47 = new VestasWTG( TagNameRow, ConfigRow);
// There are several other delegates instantiated that are not shown
// 10 min datarow delegate
V47.WTGDataRowR eady_10 += new
Trillium.WindSp ace.VestasWTG.W TGDataRowReady1 0Delegate(WTGDa taRowReady10Han d
ler); break; default: break;
}
}
}

Nov 17 '05 #2
Dale:
Thanks for the answer. I think I'm kinda using your second approach. Please
take a look at the following delegate event handler code:

private ArrayList WTGData_10 = new ArrayList();

private void WTGDataRowReady 10Handler( WTGDataGroup_10 myWTGData )
{
WTGData_10Timer .Enabled = false;
try
{
WTGData_10.Add( myWTGData);
}
catch( Exception ex)
{
WriteToEventLog s.logWrite("WTG DataRowReady10H andler: " +
ex.Message, TrilliumConstan ts.WTGLog);
}
WTGData_10Timer .Interval = 9999;
WTGData_10Timer .Enabled = true;
}

Let me explain my madness:
There may be fifty or more V47 objects. Every ten minutes each V47 has a
struct of data (myWTGData) that needs to be written to a SQL database. My
thouught was rather then having each object opening a SQL connection, writing
a single record and closing, I would send each struct to the main form event
handler. The event handler quickly stuffs them into a collection. Once all
the delegates have completed, the timer in the event handler can then
complete and finally the collection is written to the database. What do you
think of this?

FredC

"Dale Preston" wrote:
If every instance of a V47 handles the event in exactly the same way and
there is no variation, I would suggest handling the event directly in the
class rather than exposing the event. Alternatively, handle the event in
the class and still expose the event and let the class consumers add more to
the process if they need to.

HTH

Dale Preston
MCAD, MCDBA, MCSE

"FredC" <Fr***@discussi ons.microsoft.c om> wrote in message
news:CC******** *************** ***********@mic rosoft.com...
S Name Microsoft Windows XP Professional
Version 5.1.2600 Service Pack 2 Build 2600
Total Physical Memory 1,024.00 MB

MDE 2003 Version 7.1.3008
.NET Framework 1.1 Version 1.1.4322 SP1
Microsoft Visual C# .NET 69462-335-0000007-18707
Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3

The code below shows the instantiation of multiple V47 objects. These
objects have several delegates that are fired by private V47 methods.

These
private methods run on timers. In the code below, as well as instantiating
multilpe V47 objects I have instantiated an event handler
(V47.WTGDataRow Ready_10) for each V47 object. Apperarently, one handler

for
each V47 object. IS THIS THE CORRECT WAY TO DO THIS?

foreach ( DataRow TagNameRow in DSTagNames.Tabl es["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in
DSWTG.Tables["TurbineConfigu ration"].Rows )
{
switch ( ConfigRow["Turbine Type"].ToString())
{
case "V47":
// instantiate a new turbine
VestasWTG V47 = new VestasWTG( TagNameRow, ConfigRow);
// There are several other delegates instantiated that

are
not shown
// 10 min datarow delegate
V47.WTGDataRowR eady_10 += new

Trillium.WindSp ace.VestasWTG.W TGDataRowReady1 0Delegate(WTGDa taRowReady10Han d
ler); break;
default: break;
}
}
}


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2535
by: emma middlebrook | last post by:
Hi I want to find out what objects are due to receive an event i.e. those that have added themselves as an event handler via +=. Yes, it's a little pointless perhaps (or can anyone give some good uses for this?!!). How do I do this for an event on a class I implement? Also, how may I do this for an event in the .Net framework e.g. a control?
3
2940
by: Bob | last post by:
C# newbie here.... studying about delegates and now events. I just don't get the purpose of declaring events using the event keyword. What does this do for me that can't be done using only delegates?
1
55864
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the event and what are the functions hooked, but i need to make sure that i unwired all of them at once. I could unwire event handlers if i knew the functions: button.Click += new EventHandler(OnMyBtnClick); // Wire event handler ....
3
1622
by: Peter Oliphant | last post by:
In general, some controls can have events handlers attached to them, typically via the += operator. How does one remove an event handler added this way? Or, just as good for my purposes, how can one remove all event handlers attached?
27
1937
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the <Serialized()> attribute on the class and away you go. As Homer would say - "D'Oh" The root of my problem lies in the way VB implements Events and the fact that you can't apply the <NonSerialized> attribute to the little rascals.
13
3501
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through the sender events and tries to get methods from the receiver that match the pattern. For each one...
11
3247
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
31
2575
by: Scott M. | last post by:
Am I correct in thinking that because C# doesn't have the "Handles" keyword that VB .NET does, we have to register event delegates manually in C#, whereas in VB .NET using "Handles" takes care of registering the event delegate for us behind the scenes? In other words, *if* C# did have a "Handles"-type keyword, would we still need to register an event delegate?
1
4601
by: Phil Townsend | last post by:
I have an application that needs to respond to events that occur outside of the application itself. My project, called "ShowDetection" declares the event. I have a console app called "TestEvent" that I would like to use to test the event handler. Any action in the console app would be acceptable, such as a keystroke. I am at a loss on how to go about detecting and raising this event in my Winform. cn anybody help? Thanks!
0
8438
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
8348
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,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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
8636
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...
0
7376
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
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.