kejpa Posted September 8, 2005 Posted September 8, 2005 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 Quote
mhildner Posted September 8, 2005 Posted September 8, 2005 Hope I understand you correctly. Do you want to have the BinaryLog listen to events in other objects? Quote
kejpa Posted September 9, 2005 Author Posted September 9, 2005 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 Quote
Administrators PlausiblyDamp Posted September 9, 2005 Administrators Posted September 9, 2005 Could you not just use AddHandler to connect up the event handler at runtime? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
kejpa Posted September 9, 2005 Author Posted September 9, 2005 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 Quote
Administrators PlausiblyDamp Posted September 9, 2005 Administrators Posted September 9, 2005 If you fail to remove an event handler then it simply means the event handler is fired when it wasn't required. If you remove the object the handler is attached to then there is no real consequence. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mhildner Posted September 9, 2005 Posted September 9, 2005 Sounds like you should be using the observer pattern. This article first discusses how to implement using interfaces, then the section titled "Observer Pattern in the .NET Framework" discusses how to achieve using events and delegates. I'd use the event/delegate way. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/observerpattern.asp Mike Quote
kejpa Posted September 12, 2005 Author Posted September 12, 2005 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 Quote
mhildner Posted September 12, 2005 Posted September 12, 2005 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 Quote
Leaders snarfblam Posted September 12, 2005 Leaders Posted September 12, 2005 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. Quote [sIGPIC]e[/sIGPIC]
Leaders snarfblam Posted September 13, 2005 Leaders Posted September 13, 2005 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. Quote [sIGPIC]e[/sIGPIC]
mhildner Posted September 13, 2005 Posted September 13, 2005 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.