Page 1 of 1

Understanding Events with the Crying Baby Example Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Posted 28 June 2011 - 11:28 AM

Crying Baby Example

Baby
This is a simple baby, it has two event

1. Internal I_Cry

2. Public Crying


Public Class Baby
#Region "Birth"
  Public Sub New(NameOfBaby As String)
    mName = NameOfBaby
    WhenToCryNext()
  End Sub
#End Region


#Region "Events"
#Region "Internal" ' Only this baby knows about.
  Protected Event i_Cry()
#Region "Handle Private Events"

  Private Sub Baby_Crying(b As Baby) Handles Me.Crying
    Console.WriteLine("Wha Whha")
  End Sub
  Private Sub _Cry() Handles Me.i_Cry
    Me.mC = True
    Console.WriteLine("Cry Now: {0} ", Name)
    RaiseEvent Crying(Me)
  End Sub
#End Region
#End Region
#Region "Public"
  Public Event Crying(b As Baby)
#End Region
#End Region

#Region "Properties"
  Public ReadOnly Property Name As String
    Get
      Return Me.mName
    End Get
  End Property
  Public ReadOnly Property IsCrying() As Boolean
    Get
      Return Me.mC
    End Get
  End Property
#End Region

#Region "External Stimuli"
  Public Sub Feed(FedBy As Parent)
    Me.mC = False
    WhenToCryNext()
    Console.WriteLine("Happy; {0} (Fed By: {1}), I'll Sleep", Me.Name, FedBy.Name)
  End Sub
#End Region


#Region "Baby Internal Logic"
  Protected mName As String
  Protected mT As System.Threading.Timer
  Protected mR As New Random
  Protected mC As Boolean = False

  Private Sub WhenToCryNext() ' 
    Me.mT = New System.Threading.Timer(New Threading.TimerCallback(AddressOf MeGoingToStartCrying), Nothing, mR.Next(1000, 10000), Threading.Timeout.Infinite)
  End Sub

  Private Sub MeGoingToStartCrying()
    RaiseEvent i_Cry()
  End Sub
#End Region

End Class



Parent
The parent's job is simple, listen (reactive) to baby they're in responsible for, to cry and feed it.

Public Class Parent
  Protected mDep As New List(Of Baby)
  Protected mName As String
  Public Sub New(ByVal Name As String)
    mName = Name
  End Sub
  Public ReadOnly Property Name
    Get
      Return mName
    End Get
  End Property

  Public Sub AddBaby(ThisBay As Baby)
    AddHandler ThisBay.Crying, AddressOf HandleCryingBaby
  End Sub

  Private Sub HandleCryingBaby(ThisBaby As Baby)
    Console.WriteLine("Crying: {0}", ThisBaby.Name)
    Console.WriteLine("Feeding; {0}", ThisBaby.Name)
    ThisBaby.Feed(Me)
  End Sub
End Class




The Simulation.

Module Module1

  Sub Main()
    Dim babies = {New Baby("Alice"),
                  New Baby("Bob"),
                  New Baby("Charlie")}
    Dim p1 As New Parent
    p1.AddBaby(babies(0))
    p1.AddBaby(babies(1))
    While True
      babies.Where(Function(B)/> b.IsCrying).ToList.ForEach(Sub(B)/> Console.WriteLine("   {0} (Crying)", b.Name))
      Threading.Thread.Sleep(500)
    End While

  End Sub

End Module



Conclusions
Spoiler


If only real babies were this simple.

This post has been edited by AdamSpeight2008: 28 June 2011 - 11:38 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Understanding Events with the Crying Baby Example

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Posted 28 June 2011 - 11:40 AM

Good tutorial and simple, I was thinking about writing something on events and delegates ;)
Question: Why spoiler is not showing?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1