Hello everyone
I am perusing knowledge on the use of delegates; any assistance would be greatly appreciated. Lets take an example of a community of people, they are born, live a while, then die. The community would get notifications of birth and deaths, upon a death notice, they would bury the person. Below is my sample code, I am looking for a critique on its use and how I would go about doing this better. Thanks
Namespace bla
Public Delegate Sub Died(ByVal thisperson As Person
Public Delegate Sub Born(ByVal thisperson As Person
Public Class PopGe
Private PersonDied As blah.Die
Private PersonBorn As blah.Bor
Private col As Collection = New Collectio
Private col1 As Collection = New Collectio
Private WithEvents atimer As System.Timers.Time
Friend Sub Dieded(ByVal thisperson As Person
Debug.Write(thisperson.Index.ToString & ": has died" & vbCrLf
col1.Add(thisperson.Index, "Item:" & thisperson.Index.ToString
End Su
Friend Sub Borned(ByVal thisperson As Person
Debug.Write(thisperson.Index.ToString & ": was born" & vbCrLf
End Su
Public Sub New(
atimer = New System.Timers.Time
atimer.Interval =
atimer.Enabled = Tru
PersonBorn = New blah.Born(AddressOf Borned
PersonDied = New blah.Died(AddressOf Dieded
Dim i As Int3
For i = 1 To 10
'System.Threading.Thread.Sleep(50
col.Add(New Person(i, PersonBorn, PersonDied), "Item:" & i.ToString
Next
End Su
Private Sub atimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles atimer.Elapse
Dim i As Int3
Dim al As ArrayList = New ArrayLis
For Each i In col
al.Add(i
col.Remove("Item:" & i.ToString
'System.Threading.Thread.Sleep(50
Nex
For Each i In a
col1.Remove("Item:" & i.ToString
Nex
End Su
End Clas
Public Class Perso
Private _gender As Strin
Private WithEvents atimer As System.Timers.Time
Public ReadOnly Property Gender() As Strin
Ge
Return _gende
End Ge
End Propert
Private _age As Intege
Public ReadOnly Property Age() As Intege
Ge
Return _ag
End Ge
End Propert
Private _index As Int3
Friend ReadOnly Property Index() As Intege
Ge
Return _inde
End Ge
End Propert
Private _born As blah.Bor
Private _died As blah.Die
Public Sub New(ByVal Index As Int32, ByVal BornEvent As blah.Born, ByVal DiedEvent As blah.Died
_age = -
_born = BornEven
_died = DiedEven
_index = Inde
atimer = New System.Timers.Time
atimer.Interval = 50
atimer.Enabled = Tru
End Su
Protected Overrides Sub Finalize(
MyBase.Finalize(
Debug.Write(_index.ToString & ": removed" & vbCrLf
atimer.Enabled = Fals
End Su
Private Sub atimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles atimer.Elapse
'Assses life and deat
'or just get olde
atimer.Enabled = Fals
_age = _age +
If _age = 0 The
_born.Invoke(Me
atimer.Enabled = Tru
Exit Su
End I
If _age >= 100 The
_died.Invoke(Me
Els
atimer.Enabled = Tru
Exit Su
End I
End Su
End Clas
End Namespac