Solved

Nested Event Handling

Posted on 2007-12-03
4
465 Views
Last Modified: 2013-11-26
I have a class that contains a collections of objects (another class).

In the base collection I have several event handlers that I want to be able to pass through the collection class to the form that will call the creation of the collection.

I have the events created in the base class, I am not sure how to pass the events through the collection class.

In the attached snippet I have quickly written a small sample of what I am trying to do.

My thought is that I will have to add Event Handlers to the collection class to capture the base classes events.  I would have to create corresponding events in my collection class and then I would raise the events in the handlers I created in the collection class.  This would allow the form that I want to have finally handle the events.

Is there any way I can just see the base classes events from the form?  this seems like it is a little much to do to pass events through.

any help would be appreciated.


Example:
 
Public objClass as Class
    'Events
    Public Event StartMakingClass ()
    Public Event FinishedMakingClass ()
    
    Public Sub New()
        RaiseEvent StartMakingClass ()
        'Do some stuff, may take a while
        RaiseEvent FinishedMakingClass ()
    End Sub
End Class
 
Public Class objClassCollection
    Private mobjClasses as New System.Collection.ArrayList
    Default Public Property objClassClasses (Index as Integer) as objClass 
       Get
            Return CType(mobjClasses(Index), objClass)
        End Get
        Set(ByVal Value As objClass)
            mobjClasses(Index) = Value
        End Set
    End Property
    
    Public Sub New()
         For x as int16 = 1 to 100
             Dim aobjClass as New objClass
             mobjClass.Add(aobjClass)
         Next x
    End Sub
End Class

Open in new window

0
Comment
Question by:lgropper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
jmundsack earned 500 total points
ID: 20452344
It's been a while since you posted this, so I don't know if you already came up with a solution.  I've been hesitant to contribute because I haven't learned dotnet yet.  But I had some ideas on what you could do, if my memory about dotnet capabilities is correct.

If I remember correctly, dotnet supports the ability to define parameterized constructors -- that is, when instantiating an object, you can pass a parameter to it which can be utilized by the constructor (as opposed to VB6, which requires that the object be instantiated before you can pass anything into it).

If that is correct, then you could do something like this:

1. In objClassCollection, define "callback" procedures for your two events that accept an objClass argument:

(VB6 syntax)
Public Sub StartMakingClass(Obj As objClass)
End Sub
Public Sub FinishedMakingClass(Obj As objClass)
End Sub

2. In objClass, create a parameterized constructor that accepts an objClassCollection argument:

(I have no idea what the syntax would be, but essentially what you want to do is store the reference to the objClassCollection object, inside of each objClass instance.)

3. In objClass, instead of raising an event, directly call the StartMakingClass or FinishedMakingClass procedures of the objClassCollection object, which you've got a reference to inside each objClass object.

(VB6 syntax -- assuming the local variable holding the objClassCollection object is called mParent)

mParent.StartMakingClass Me
(or)
mParent.FinishedMakingClass Me

4. In the objClassCollection object define the two events, then raise them from within the StartMakingClass and FinishedMakingClass procedures:

(VB6 syntax)
Public Event StartMakingClass ()
Public Event FinishedMakingClass ()
Public Sub StartMakingClass(Obj As objClass)
    RaiseEvent StartMakingClass
End Sub
Public Sub FinishedMakingClass(Obj As objClass)
    RaiseEvent FinishedMakingClass
End Sub

Now your form would declare the objClassCollection object WithEvents so that it can respond to these "events" from the individual objClass objects, channeled through the objClassCollection object.

Not sure if you can use any of this, but hope it helps.
0
 
LVL 13

Expert Comment

by:jmundsack
ID: 20452687
Or you could pass a reference to the form, into each objClass, and then put the callback procedures on the form instead of the collection.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20632923
Forced accept.

Computer101
EE Admin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question