.NET Forum / Languages / VB.NET / April 2007
Interface Inheritence Help
|
|
Thread rating:  |
Wayne Pedersen - 11 Apr 2007 19:55 GMT Need some help - and I may be doing this wrong, so please correct and suggest!
I'm learning the MVP method, which I seem to have a good grasp of. Now I am trying something a bit more advanced. I'm declaring an event in a interface. I'm consuming the interface via another inherited Interface.
I'd like to have my classes respond to this 'one' event...
Based on my below example code, I'd like both Classes Test1 and Test2 to Respond to EventToShare when it gets raised. How do I do this? I have things setup as below...
Example Code:
Public Interface IView_Base
Event EventToShare(SomeText as String)
End Interface
Public Interface IView_Test1 Inherits IView_Base
Event AnotherEvent
End Interface
Public Interface IView_Test2 Inherits IView_Base
Event AnotherEvent2 End Interface
Public Class Test1
dim myObj as IView_Test1
Public Sub New(View as IView_Test1) myObj = View addhandler myObj.EventToShare, AddressOf Sub1 End Sub
Private Sub Sub1(Text as String) 'Do something with the text End Sub End Class
Public Class Test2
dim myObj as IView_Test2
Public Sub New(View as IView_Test2) myObj = View addhandler myObj.EventToShare, AddressOf Sub2 End Sub
Private Sub Sub2(Text as String) 'Do something else with the text over here End Sub End Class
Thanks for the help!
Wayne P.
Charlie Brown - 11 Apr 2007 20:39 GMT Wayne, The technique you are using is not interfaces, but inheritance. To implement your classes as interfaces do the following.
Public Interface IView_Base
Event EventToShare(SomeText as String)
End Interface
Public Interface IView_Test1 Implements IView_Base
Event AnotherEvent
End Interface
Public Interface IView_Test2 Implements IView_Base
Event AnotherEvent2 End Interface
Wayne Pedersen - 11 Apr 2007 21:00 GMT Thanks, but I'm not quite sure this is what I need. I performed your suggestion (assuming you meant to make things classes, I cannot implement an Interface inside an Interface).
I am implementing my interfaces in classes, I did not include that as part of my example code.
Expanding on this, I have the IView_Base, which IView_Test1 Inherits. IView_Test2 also Inherits IView_Base.
What I am looking to make happen, is when the Event EventToShare is raised, both Presenter_Test1 and Presenter_Test2 respond to the single event call.
Public Class Class1 implements IView_Test1 Public Event EventToShare(ByVal SomeText As String) Implements IView_Test1.EventToShare Public Sub New() RaiseEvent EventToShare("Event From Class 1") End Sub End Class
Public Class Class2 Implements IView_Test2 Public Event EventToShare(ByVal SomeText As String) Implements IView_Test2.EventToShare
Public Sub New() Dim Presenter as new Presenter_Test2(Me) RaiseEvent EventToShare("Event From Class 2") End Sub End Class
Public Class Presenter_Test1
Dim view As IView_Test1
Public Sub New(ByVal CurView As IView_Test1) view = CurView Init() End Sub
Private Sub Init() AddHandler View.EventToShare, AddressOf Test End Sub
Private Sub Test(ByVal Text As String) 'Do Something with the Text End Sub
End Class
Public Class Presenter_Test2
Dim view As IView_Test2
Public Sub New(ByVal CurView As IView_Test2) view = CurView Init() End Sub
Private Sub Init() AddHandler View.EventToShare, AddressOf Test End Sub
Private Sub Test(ByVal Text As String) 'Do Something ELSE with the Text End Sub
End Class
Thanks!
Wayne P.
> Wayne, > The technique you are using is not interfaces, but inheritance. To [quoted text clipped - 18 lines] > Event AnotherEvent2 > End Interface Charlie Brown - 11 Apr 2007 21:10 GMT Here you go, this is how I would implement it. Which is basically identical to the way you did it, just more vb-ish.
Public Interface IView_Base
Event EventToShare(ByVal SomeText As String)
End Interface
Public Interface IView_Test1 Inherits IView_Base
Event AnotherEvent()
End Interface
Public Interface IView_Test2 Inherits IView_Base
Event AnotherEvent2() End Interface
Public Class Class1 Implements IView_Test1
Public Event EventToShare(ByVal SomeText As String) Implements IView_Test1.EventToShare Public Event AnotherEvent() Implements IView_Test1.AnotherEvent
Public Sub New() RaiseEvent EventToShare("Event From Class 1") End Sub
End Class
Public Class Class2 Implements IView_Test2
Public Event EventToShare(ByVal SomeText As String) Implements IView_Test2.EventToShare Public Event AnotherEvent2() Implements IView_Test2.AnotherEvent2
Public Sub New() Dim Presenter As New Presenter_Test2(Me) RaiseEvent EventToShare("Event From Class 2") End Sub
End Class
Public Class Presenter_Test1
Private WithEvents view As IView_Test1
Public Sub New(ByVal CurView As IView_Test1) view = CurView End Sub
Private Sub Test(ByVal Text As String) Handles view.EventToShare 'Do Something with the Text End Sub
End Class
Public Class Presenter_Test2
Private WithEvents view As IView_Test2
Public Sub New(ByVal CurView As IView_Test2) view = CurView End Sub
Private Sub Test(ByVal Text As String) Handles view.EventToShare 'Do Something ELSE with the Text End Sub
End Class
Wayne Pedersen - 11 Apr 2007 21:42 GMT Great!
Moving forward, Why when the event is raised, is Sub Test, in BOTH Class Presenter_Test1 and Class Presenter_Test2 not called.
The event is either one or the other, depending on which presenter is was called in. I have a need to have Presenter_Test1 AND Presenter_Test2 respond to the event, regardless of where it was called from.
Thanks!
Wayne
> Here you go, this is how I would implement it. Which is basically > identical to the way you did it, just more vb-ish. [quoted text clipped - 72 lines] > > End Class Charlie Brown - 12 Apr 2007 03:49 GMT Thats a great question... too great for me. In your current setup, I don't believe you can achieve such a thing, since interfaces are never instanced i don't believe it's possible. If you were passing in a pointer of the same object to both presenter classes than yes, but if you need to pass different interfaces to each presenter class, then no. If I may ask, what is the solution you are designing for, maybe I could offer an alternative?
Wayne Pedersen - 12 Apr 2007 14:31 GMT I am implementing a document management system, simple in nature. I have two user controls: Folders on the left, Files on the rights. Using MVP architecture as we have been discussing, each control is independent other than the fact that their interfaces inherit from the same base interface (IView_Base).
I want to create a commonality in the base interface so when the event gets called, both, or 'all interfaces and classes that handle the event' (which can mean more than these two in the future) are raised.
Maybe this is more of a MVP Architecture issue and I need to repost?
Thanks for your efforts!
Wayne P.
> Thats a great question... too great for me. In your current setup, I > don't believe you can achieve such a thing, since interfaces are never [quoted text clipped - 3 lines] > no. If I may ask, what is the solution you are designing for, maybe I > could offer an alternative? Charlie Brown - 12 Apr 2007 15:26 GMT Wayne, do you have an internet reference to the "MVP Architecture", I am unfamiliar with what you are referring to.
As far as your solution/problem, I would recommend creating a Static (Shared) class with Static (Shared) events and consuming those events by the user controls. When the static event fires, both controls will react.
Public Class StaticClass
Public Shared Event DocumentChanged(ByVal args() As String)
Public Shared Sub FireEvent(ByVal args() As String) RaiseEvent DocumentChanged(args) End Sub
End Class
Public Class Control1 Inherits Control
Public Sub New() AddHandler StaticClass.DocumentChanged, AddressOf DocumentChanged End Sub
Private Sub DocumentChanged(ByVal args() As String) 'event code End Sub
End Class
Public Class control2 Inherits Control
Public Sub New() AddHandler StaticClass.DocumentChanged, AddressOf DocumentChanged End Sub
Private Sub DocumentChanged(ByVal args() As String) 'event code End Sub
End Class
Public Class Test1
Public Sub dosomething() Dim args() As String = {} StaticClass.FireEvent(args) End Sub
End Class
Wayne Pedersen - 12 Apr 2007 15:39 GMT Thanks for your help.
Your example gives me a few ideas. If I need additional help, I will repost.
Here's a link to MVP (Model-View-Presenter), a design pattern.
http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx
Thanks!
Wayne
> Wayne, do you have an internet reference to the "MVP Architecture", I > am unfamiliar with what you are referring to. [quoted text clipped - 50 lines] > > End Class Charlie Brown - 11 Apr 2007 20:48 GMT If you seem my last post, ignore that.
Ok, you need to create an object that will Implement your interfaces.
Public Class CustomObject Implements IView_Test2
.....code will be created automagically to handle events
End Class
Public Class Test2 dim myObj as CustomObject
Public Sub New(View as IView_Test2) myObj = View addhandler myObj.EventToShare, AddressOf Sub2 End Sub
Private Sub Sub2(Text as String) 'Do something else with the text over here End Sub End Class
Charlie Brown - 11 Apr 2007 20:56 GMT When you implement an interface into a class, your designer window will create the proper links for the events.
Public Class CustomObject Implements IView_Test2
Public Event AnotherEvent() Implements iView_Test1.AnotherEvent Public Event AnotherEvent2() Implements iView_Test2.AnotherEvent2
End Class
Use it in your class like so...
Public Class Test2 private myObj as IView_Test2
Public Sub New(View as IView_Test2) myObj = View addhandler myObj.EventToShare, AddressOf Sub2 End Sub
Private Sub Sub2(Text as String) 'Do something else with the text over here End Sub End Class
Wayne Pedersen - 11 Apr 2007 21:03 GMT Thanks -
See my Reply in earlier post - I understand what you are saying here. I'm doing this with no difficulty. Thanks for making this clearer.
If I need to explain myself better, please let me know!
Thank you for your help!
Wayne
> When you implement an interface into a class, your designer window > will create the proper links for the events. [quoted text clipped - 21 lines] > End Sub > End Class
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|