By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Class and Collection Cross Calling

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

alanb wrote:
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.
--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a
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"

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

"alanb" <ab**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 21 '05 #3

P: n/a
Thanks Larry, works perfectly!

Nov 21 '05 #4

P: n/a
Many thanks Carsten

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.