Class and Collection Cross Calling

A

alanb

I have one object which holds a collection of other objects. I'll make
one up as my specific example is too abstract and too much code:


Public Class Job


Dim CandidateCollection As New Collection


Public Sub AddCandidate(ByVal aCandidate As Candidate)
CandidateCollection .Add(aCandidate)
End Sub


Public Sub AProcedure()
' code here
End Sub


End Class


Public Class Candidate


Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
' now trigger Job AProcedure
End Set
End Property


End Class


What I'm trying to do is trigger an event in the Set Property Name to
run the sub in the class Job, i.e. when I run code:


Job.Candidate(1).Name = "Smith"


the sub AProcedure in Job runs. I suspect the answer may be a
reconstruction of the classes and/or collection, or is this legitimate
code with an easy way to call the sub?


TIA,


Alan
 
L

Larry Lard

alanb said:
I have one object which holds a collection of other objects. I'll make
one up as my specific example is too abstract and too much code:


Public Class Job


Dim CandidateCollection As New Collection


Public Sub AddCandidate(ByVal aCandidate As Candidate)
CandidateCollection .Add(aCandidate)
End Sub


Public Sub AProcedure()
' code here
End Sub


End Class


Public Class Candidate


Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
' now trigger Job AProcedure
End Set
End Property


End Class


What I'm trying to do is trigger an event in the Set Property Name to
run the sub in the class Job, i.e. when I run code:


Job.Candidate(1).Name = "Smith"


the sub AProcedure in Job runs. I suspect the answer may be a
reconstruction of the classes and/or collection, or is this legitimate
code with an easy way to call the sub?

In Candidate, add an event NameChanged. Raise this event in the Set of
Name.

In Job, in AddCandidate, do

AddHandler aCandidate.NameChanged, AddressOf AProcedure

Done :)

It will probably be useful to have the NameChanged event have the
Candidate as a parameter so that AProcedure knows which candidate's
name has just changed. Remember that the signature of AProcedure (or
whatever you hook up with AddHandler) must match the signature of
NameChanged.

Also if candidates ever get removed from a Job's CandidateCollection,
their NameChanged event should be unhooked with RemoveHandler.
 
C

CT

Something like this will work:

Public Delegate Sub AnEventHandler(ByVal sender As Object, ByVal e As
EventArgs)

Public Class Job
Public CandidateCollection As New Collection

Public Sub AddCandidate(ByVal aCandidate As Candidate)
CandidateCollection.Add(aCandidate)
End Sub

Public Sub AProcedure(ByVal sender As Object, ByVal e As EventArgs)
' code here
Console.WriteLine("Test")
End Sub
End Class

Public Class Candidate
Public Event AnEvent As EventHandler

Private mName As String

Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
' now trigger Job AProcedure
Dim e As New EventArgs
OnAnEvent(e)
End Set
End Property

Protected Overridable Sub OnAnEvent(ByVal e As EventArgs)
RaiseEvent AnEvent(Me, e)
End Sub
End Class

Dim jb As New Job
Dim cand As New Candidate

AddHandler cand.AnEvent, AddressOf jb.AProcedure
jb.AddCandidate(cand)
jb.CandidateCollection(1).Name = "Smith"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top