Instantiating objects and event handlers for delegates

G

Guest

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.WTGDataRowReady10Delegate(WTGDataRowReady10Handler); break;
default: break;
}
}
}
 
D

Dale Preston

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 said:
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.WTGDataRowReady10Delegate(WTGDataRowReady10Hand
ler); break;
 
G

Guest

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("WTGDataRowReady10Handler: " +
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 said:
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 said:
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.WTGDataRowReady10Delegate(WTGDataRowReady10Hand
ler); break;
default: break;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top