Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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.

AddHandler yourobject.yourevent, AddressOf handlermethod

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...