Events and VB.Net

Geeky

Newcomer
Joined
Nov 28, 2003
Messages
1
Hi All, I'm new to object oriented programming and am trying my hand at VB.net, but seemed to have missed a fundamental point somewhere along the line.

My understanding is that objects communicate with eachother by raising and consuming events. I have the following two issues

1. I dont want to create an instance of class1 in class2 if i dont need to, but need to find a way to consume an event in class2 that was raised by class1. eg. class1=form1, class2=blobclass. Form1 raises event
MyEvent and i want this event to be consumed in class2
ie. Public MyConsumer() Handles Form1.Myevent. The problem is that i cant get the handles statement to recognise form1. I know i'm not thinking about this the right way, but could someone show me the light please.

2. A separate issue, but i will keep class1 and class2 as above.
In class1 ie. Form1, I have declared and initialised 26 instances of class2(its for a number puzzle project I'm working on if you were wondering why there are so many instances of class2). Event someevent is raised in class2 which needs to be handled in class1. ie
in form1:

Dim blob1,blob2,blob3....blob26 as new blobClass()

Private sub ConsumeBlobEvents() handles blob1.someevent, blob2.someevent,.....blob26.someevent

Do i need to type out all 26 variables in this handles statement?
Is there some shorthand way to handle this?
What if i am creating blob instances dynamically?How do i handle all "someevent" raised by blobClass in one procedure in form1, irrespective of which instance raised the "someevent"?

I apologise for what must seem a very basic question and thank you in advance for your help
 
1. You need an instance of the class to create a handler for it. Simply using the class name will not work, without an object that holds its instance. You could intercept an event without a class instance if the event was declared as shared.
2. Everytime you create your new object use the AddHandler keyword to add an event handler for the object.
Visual Basic:
AddHandler yourobject.yourevent, AddressOf handlermethod
 
Back
Top