Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I'm planning on having a BinaryLog class for all the logging I need and I would like to be able to attach objects and their events dynamically to different instances of my BinaryLogs.

 

The logee events all implement iBinaryLogItem and typically look like

Public Event NewData(ByVal Values As UnitData) ' UnitData is implementing iBinaryLogItem

 

What I want to be able to do is something like...

Dim oUnitLog as new BinaryLog ("c:\temp\Units.log")
oUnitLog.Attach(objUnit(1), objUnit(1).NewData)

Any object with an event matching the typical event above should be allowed to be attached. (Maybe I need some overloaded procedures as well further down the lane)

 

This seems like a good idea in order to keep the different classes independent of each other.

 

TIA

/Kejpa

Posted
Hope I understand you correctly. Do you want to have the BinaryLog listen to events in other objects?

Correct!

I want to attach objects to the BinaryLog and have it handle events, ie write the variable implementing BinaryLogItem to a log file.

 

TIA

/Kejpa

Posted
Could you not just use AddHandler to connect up the event handler at runtime?

Hmmmmm, never thought about that.....

I guess I could, I'm a little worried though that I might miss some RemoveHandler statement. What effects does that have on the system?

 

Regards

/Kejpa

Posted

Thanx Mike!

I needed that one ;)

And so I've done my home work and read it thoroughly and I guess I have understood that I should use the common event handler pattern (sender as object, e as WhatEverEventArgs)

But I can't figure out why I need to declare a delegate for the events?

 

Why should I...

    'declare a delegate for the event
   Delegate Sub AskPriceChangedHandler(ByVal sender As Object, ByVal e As 
           AskPriceChangedEventArgs)

   'declare the event using the delegate
   Public Event AskPriceChanged As AskPriceChangedHandler

... instead of just ....

    'declare the event 
   Public Event AskPriceChanged (ByVal sender As Object, ByVal e As 
           AskPriceChangedEventArgs)

... which is my current approach?

 

Regards

/Kejpa

Posted

Hello Kejpa,

 

You know, I'm not really sure - do they both behave the same? I'm thinking it's a difference between VB.NET and C#. VB.NET seems to do more behind the scenes and you don't have to do that much manual work.

 

If both ways work the same, you're probably saving some code doing it your way. But I'm not an expert at VB.NET and a newbie when it comes to delegates - although I have managed to get some code to work :)

 

Mike

  • Leaders
Posted
Thanx Mike!

I needed that one ;)

And so I've done my home work and read it thoroughly and I guess I have understood that I should use the common event handler pattern (sender as object, e as WhatEverEventArgs)

But I can't figure out why I need to declare a delegate for the events?

 

Why should I...

    'declare a delegate for the event
   Delegate Sub AskPriceChangedHandler(ByVal sender As Object, ByVal e As 
           AskPriceChangedEventArgs)

   'declare the event using the delegate
   Public Event AskPriceChanged As AskPriceChangedHandler

... instead of just ....

    'declare the event 
   Public Event AskPriceChanged (ByVal sender As Object, ByVal e As 
           AskPriceChangedEventArgs)

... which is my current approach?

 

Regards

/Kejpa

Your method (the second one) or declaring events will work just fine, but declaring a delegate type means that other parts of the program can pass around a function pointer (i.e. delegate) that points to a handler. Perhaps the method you are using exists for VB6 code compatability. I don't know. But it works just fine.

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted

I just accidentally discovered that an event declared with parameters (VB6 style) as opposed to with a delegate will implicity declare a delegate, which, while it does not appear in the class view or intellisense, can be used and does not cause a compiler error. The name given to the delegate is eventnameEventHandler.

Class TestClass
   'Declare Event
   Public Event TestEvent(Parameters As Types)
   'This delegate type is not explicitly defined but will not cause any errors
   Public Dim MyDelegateVariable As TestEvent[u]EventHandler[/u]
End Class

Thought I would add that to the thread since it seems quite relevant. Under the hood both event declarations are actually the same except that the VB6 way implicity declares your delegate for you.

[sIGPIC]e[/sIGPIC]
Posted

Hello marble_eater,

 

Those last two posts were excellent. I might go play with some MSIL - it'd be interesting to check this out in VB.NET and C# and see what the compilers do.

 

Regards,

Mike

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...