473,387 Members | 1,495 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,387 software developers and data experts.

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.WTGDataRowReady_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.Tables["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in
DSWTG.Tables["TurbineConfiguration"].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.WTGDataRowReady_10 += new
Trillium.WindSpace.VestasWTG.WTGDataRowReady10Dele gate(WTGDataRowReady10Handler); break;
default: break;
}
}
}
Nov 17 '05 #1
2 2255
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***@discussions.microsoft.com> wrote in message
news:CC**********************************@microsof t.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.WTGDataRowReady_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.Tables["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in
DSWTG.Tables["TurbineConfiguration"].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.WTGDataRowReady_10 += new
Trillium.WindSpace.VestasWTG.WTGDataRowReady10Dele gate(WTGDataRowReady10Hand
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 WTGDataRowReady10Handler( WTGDataGroup_10 myWTGData )
{
WTGData_10Timer.Enabled = false;
try
{
WTGData_10.Add(myWTGData);
}
catch( Exception ex)
{
WriteToEventLogs.logWrite("WTGDataRowReady10Handle r: " +
ex.Message, TrilliumConstants.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***@discussions.microsoft.com> wrote in message
news:CC**********************************@microsof t.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.WTGDataRowReady_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.Tables["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in
DSWTG.Tables["TurbineConfiguration"].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.WTGDataRowReady_10 += new

Trillium.WindSpace.VestasWTG.WTGDataRowReady10Dele gate(WTGDataRowReady10Hand
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
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...
3
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...
1
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...
3
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...
27
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...
13
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...
11
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...
31
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...
1
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"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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...
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,...

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.