473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Class and Collection Cross Calling

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
4 942

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
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
Thanks Larry, works perfectly!

Nov 21 '05 #4
Many thanks Carsten

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeff Thies | last post by:
I have a number of elements of "some-class". I'd like to change the styles of some-class: from ..some-class{color: red; display: block} to
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
2
by: Steve Jorgensen | last post by:
I frequently find myself wanting to use class abstraction in VB/VBA code, and frankly, with the tacked-on class support in VB/VBA, there are some problems with trying to do that and have any...
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
9
by: Peri | last post by:
Dear All, I have written a common array class which has insert, search and other functions. For Example (Code in VB.NET): ' Client Class Public Class Client Public ClientCode As String ...
7
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.