Hello All,

I haven't posted a question in sometime, im hoping this is where all my previous help pays off :P

Basically starting a newish project which must be written in VB.Net (great) but I am implementing the MVC pattern pretty much throughout the entire project as there is a requirement that after the initial project is done the underlying structure (model) will be replaced with one of our own building.

So having not much related to the above I have a question or i suppose more of a debate of how to implement MVC using events or interfaces. A simple example below shows what I mean.

I have a class which holds data (model)

Public Class DataClass

    Private _name As String

    Public Property Name() As String
        Get
            return Me._name
        End Get
        Set(ByVal value As String)
            Me._name = value
        End Set
    End Property

End Class

My form (view) obviously updates according to the data held in this class. In this case the "Name" property. But I understand I can use any of the following two methods attach listeners using the Observer Pattern.

' ###
' Example 1: Interface (The way I would program with Java)

Public Interface IDataClassListener

    Sub OnNameChange(ByVal d As DataClass)

End Interface

Public Class DataClass

    ' Add a list of listeners for this object
    Private _listeners As New List(Of IDataClassListener)
    Private _name As String

    Public Property Name() As String
        Get
            return Me._name
        End Get
        Set(ByVal value As String)
            Me._name = value
            RaiseOnNameChange() ' Raise the event
        End Set
    End Property

    ' Method to add a listener to the object,
    ' there would be a remove listener method also
    Public Sub AddListener(ByVal listener As IDataClassListener)
        Me._listeners.Add(listener)
    End Sub

    ' Method to raise the method on all listening objects
    Private Sub RaiseOnNameChange()
        For Each listener As IDataClassListener In me._listeners
            listener.OnNameChange(Me)
        Next
    End Sub

End Class

' Then in my form I could implement the interface and 
' assign it as a listener on the object
Public Class MyForm
    Implements IDataClassListener

    Private _data As DataClass

    Public Sub New(ByVal data As DataClass)
        Me._data = data
        Me._data.AddListener(Me)
    End Sub

    Private Sub OnNameChange(ByVal d As DataClass) Implements IDataClassListener.OnNameChange
        ' ### Update the display on change
    End Sub

End Class
' ###
' Example 2: Event Driven

Public Class DataClass

    Delegate Sub OnNameChangeHandler(ByVal d As DataClass)
    Public Event OnNameChange As OnNameChangeHandler
    Private _name As String

    Public Property Name() As String
        Get
            return Me._name
        End Get
        Set(ByVal value As String)
            Me._name = value
            RaiseEvent Me.OnNameChange(Me) ' Raise the event
        End Set
    End Property

End Class

' Different from example 1, just add a handler to the event
Public Class MyForm

    Private _data As DataClass

    Public Sub New(ByVal data As DataClass)
        Me._data = data
        Dim d As New DataClass.OnNameChangeHandler(AddressOf Me.OnNameChange)
        AddHandler _data.OnNameChange, d
    End Sub

    Private Sub OnNameChange(ByVal d As DataClass)
        ' ### Update the display on change
    End Sub

End Class

So ok, I realize both will work, I am more comfortable with the first pattern but is there any reason why I should be using this second way? I cant seem to find a definative answer but some (very few sources) suggest the second but with no reasoning, any help is appreciated.

Event delegate Vs Interface based event.
Event delegate is better than interface based event system. You may use static or instance method as a handler with event where as with interface - implements an interface and its methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.