PC Review


Reply
Thread Tools Rate Thread

Class and Collection Cross Calling

 
 
alanb
Guest
Posts: n/a
 
      25th Sep 2005
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

 
Reply With Quote
 
 
 
 
Larry Lard
Guest
Posts: n/a
 
      25th Sep 2005

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

 
Reply With Quote
 
CT
Guest
Posts: n/a
 
      25th Sep 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>



 
Reply With Quote
 
alanb
Guest
Posts: n/a
 
      25th Sep 2005
Thanks Larry, works perfectly!

 
Reply With Quote
 
alanb
Guest
Posts: n/a
 
      25th Sep 2005
Many thanks Carsten

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class calling derived class methods Mike C# Microsoft VC .NET 8 14th Sep 2006 08:51 PM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
InitializeComponent is generating the wrong code for my custom class. Calling it's base class constructor jrhoads23@hotmail.com Microsoft Dot NET Framework Forms 0 1st Feb 2005 07:10 PM
Class X Creates Collection of Instances of Class Y. Y's need info from X Jim Frazer Microsoft C# .NET 3 16th Jun 2004 09:17 AM
RaiseEvent from a class contained in a 2nd class collection? Andrew Microsoft Excel Programming 2 6th Jan 2004 04:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:15 AM.