Click here to Skip to main content
15,896,912 members
Articles / Programming Languages / Visual Basic

Implementing Observer Pattern in VB.NET

Rate me:
Please Sign up or sign in to vote.
2.94/5 (15 votes)
1 Jan 20043 min read 92.4K   544   29  
This article shows an easy way of implementing the Observer pattern in VB.NET
Imports MV_Objects

Public Class Form1
  Inherits System.Windows.Forms.Form
  Implements MV_Objects.Observer

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents lvBr As System.Windows.Forms.ListView
  Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
  Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
  Friend WithEvents Amount As System.Windows.Forms.TextBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents CreditBtn As System.Windows.Forms.Button
  Friend WithEvents DebitBtn As System.Windows.Forms.Button
  Friend WithEvents Label1 As System.Windows.Forms.Label
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.lvBr = New System.Windows.Forms.ListView()
    Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader()
    Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader()
    Me.Amount = New System.Windows.Forms.TextBox()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.CreditBtn = New System.Windows.Forms.Button()
    Me.DebitBtn = New System.Windows.Forms.Button()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.SuspendLayout()
    '
    'lvBr
    '
    Me.lvBr.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2})
    Me.lvBr.Location = New System.Drawing.Point(0, 64)
    Me.lvBr.Name = "lvBr"
    Me.lvBr.Size = New System.Drawing.Size(488, 200)
    Me.lvBr.TabIndex = 0
    Me.lvBr.View = System.Windows.Forms.View.Details
    '
    'ColumnHeader1
    '
    Me.ColumnHeader1.Text = "Name"
    '
    'ColumnHeader2
    '
    Me.ColumnHeader2.Text = "Balance"
    Me.ColumnHeader2.Width = 174
    '
    'Amount
    '
    Me.Amount.Location = New System.Drawing.Point(56, 32)
    Me.Amount.Name = "Amount"
    Me.Amount.Size = New System.Drawing.Size(120, 20)
    Me.Amount.TabIndex = 2
    Me.Amount.Text = ""
    '
    'Label2
    '
    Me.Label2.AutoSize = True
    Me.Label2.Location = New System.Drawing.Point(8, 32)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(43, 13)
    Me.Label2.TabIndex = 3
    Me.Label2.Text = "Amount"
    '
    'CreditBtn
    '
    Me.CreditBtn.Location = New System.Drawing.Point(192, 32)
    Me.CreditBtn.Name = "CreditBtn"
    Me.CreditBtn.TabIndex = 5
    Me.CreditBtn.Text = "Credit"
    '
    'DebitBtn
    '
    Me.DebitBtn.Location = New System.Drawing.Point(280, 32)
    Me.DebitBtn.Name = "DebitBtn"
    Me.DebitBtn.TabIndex = 6
    Me.DebitBtn.Text = "Debit"
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(8, 0)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(432, 24)
    Me.Label1.TabIndex = 7
    Me.Label1.Text = "Click on a broker entry in the list and choose either debit or credit of a certai" & _
    "n amount against broker's account"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(488, 273)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.DebitBtn, Me.CreditBtn, Me.Label2, Me.Amount, Me.lvBr})
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
    Me.MaximizeBox = False
    Me.Name = "Form1"
    Me.Text = "Observer Pattern Demo"
    Me.ResumeLayout(False)

  End Sub

#End Region
  Private _brcol As New BrokerCollection()

  Public Sub UpdateBrokers(ByVal subj As Object) Implements MV_Objects.Observer.Update
    Dim br As Broker, itm As ListViewItem

    For Each itm In lvBr.Items
      br = itm.Tag

      If br.Account Is subj Then
        itm.SubItems(1).Text = br.Balance
      End If
    Next
  End Sub

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim br As Broker, i As Integer
    Dim itm As ListViewItem

    For i = 1 To 10
      br = New Broker()
      br.Name = "broker " & i
      br.Account.AddObserver(Me)
      itm = New ListViewItem(New String() {br.Name, br.Balance})
      itm.Tag = br
      lvBr.Items.Add(itm)
      _brcol.Add(br)
    Next

  End Sub

  Private Sub CreditBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreditBtn.Click
    Dim br As Broker, itm As ListViewItem

    Try
      itm = lvBr.SelectedItems(0)
    Catch
      Return
    End Try

    If Not itm Is Nothing Then
      br = itm.Tag
      Try
        br.Account.Credit(Amount.Text)
      Catch
        MsgBox("Invalid amount value")
      End Try
      Amount.Text = ""
    End If
  End Sub

  Private Sub DebitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DebitBtn.Click
    Dim br As Broker, itm As ListViewItem

    Try
      itm = lvBr.SelectedItems(0)
    Catch
      Return
    End Try

    If Not itm Is Nothing Then
      br = itm.Tag
      Try
        br.Account.Debit(Amount.Text)
        Amount.Text = ""
      Catch ex As BrokerAccount.InvalidBalanceException
        MsgBox("Balance low!")
      Catch
        MsgBox("Invalid amount value")
      End Try
    End If

  End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Belarus Belarus
I work for Logic Software (http://www.logicsoftware.net) as a lead .NET developer/architect.

Comments and Discussions