| | |
Model View Controller in VB.Net
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
Hello All,
I haven't posted a question in sometime, im hoping this is where all my previous help pays off
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)
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.
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.
I haven't posted a question in sometime, im hoping this is where all my previous help pays off

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)
VBNet Syntax (Toggle Plain Text)
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.
VBNet Syntax (Toggle Plain Text)
' ### ' 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
VBNet Syntax (Toggle Plain Text)
' ### ' 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.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
![]() |
Similar Threads
- JSP database connectivity according to Model View Controller (MVC) Model 2 (JSP)
- Model View Controller with Java (Java)
- MVC - Model View Controller (Java)
- Grid view in asp.net using C# (ASP.NET)
- Getting Started (Ruby)
- Help for learnign JSP (JSP)
- CMS....php (PHP)
Other Threads in the VB.NET Forum
- Previous Thread: Mifare Card Contactless
- Next Thread: connect sql server
| Thread Tools | Search this Thread |
.net .net2008 2008 access advanced application array basic beginner browser button buttons center checkbox click client code combo convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function html images input internet listview map mobile module monitor msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read regex remove right-to-left save search searchvb.net select serial settings shutdown socket sorting sqldatbase sqlserver survey temperature textbox timer timespan transparency txttoxmlconverter user usercontol validation vb vb.net vba vbnet visual visualbasic visualbasic.net visualstudio.net web winforms winsock wpf wrapingcode xml year






