Interface Inheritence Help

W

Wayne Pedersen

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.
 
C

Charlie Brown

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
 
C

Charlie Brown

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
 
C

Charlie Brown

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
 
W

Wayne Pedersen

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.
 
W

Wayne Pedersen

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
 
C

Charlie Brown

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
 
W

Wayne Pedersen

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
 
C

Charlie Brown

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?
 
W

Wayne Pedersen

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.
 
C

Charlie Brown

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
 

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

Top