473,594 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ 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 CandidateCollec tion As New Collection
Public Sub AddCandidate(By Val aCandidate As Candidate)
CandidateCollec tion .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 950

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 CandidateCollec tion As New Collection
Public Sub AddCandidate(By Val aCandidate As Candidate)
CandidateCollec tion .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.Name Changed, 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 CandidateCollec tion,
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 CandidateCollec tion As New Collection

Public Sub AddCandidate(By Val aCandidate As Candidate)
CandidateCollec tion.Add(aCandi date)
End Sub

Public Sub AProcedure(ByVa l sender As Object, ByVal e As EventArgs)
' code here
Console.WriteLi ne("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.CandidateCol lection(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**********@g mail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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 CandidateCollec tion As New Collection
Public Sub AddCandidate(By Val aCandidate As Candidate)
CandidateCollec tion .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
10335
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
2135
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 access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I want to send that dataset to another class module. At first it seems that the "object oriented"...
0
1352
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 templates: ------------------------------------------ in file "Model.h":
2
2269
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 type-safety as well. I thought I would share some of what I've come to think about this after dealing with it several times of late. First, an example. Let's say I have several classes, each with a string property called Name, and I have several...
7
3533
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 TypeConverter other than ReferenceConverter to be put in viewstate." I've included the <Serializable()> attribute, but I'm still getting the same error. The class is below ... as you can see it contains a Collection, two
19
4903
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 exception of custom Collection Classes. Background: I'm developing an A2K .mdb to be deployed as an .mde at my current job-site. It has several custom controls which utilize custom classes to wrap built-in controls, and add additional functionality....
26
5347
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
9
1499
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 'Key Field
7
2287
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 FindControl the only way to do it?
0
8255
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8010
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3868
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.