Dim withevents - array of classes

G

Guy Cohen

Hi all

I googled some and found that I can no longer - dim withevents myclassname()
I also understand that I can use addhandler...
But... what if I do not know the amount of classes that will be created ?....

What am I trying to do:
I have a process that scans a directory for files tranxxxxx.txt
for each transaction file I want to create a clsTransactionHandler
the class will read the file, process it, store it where it should etc etc
for each class the process needs to know when it is done (to notify the
user)...

Please advise

TIA
Guy Cohen
 
S

Stephany Young

You can no longer ...?

You never could - In VB.NET that is.

Add the handler (using AddHandler) at the point where you instantiate the
class object.
 
J

Jack Jackson

Hi all

I googled some and found that I can no longer - dim withevents myclassname()
I also understand that I can use addhandler...
But... what if I do not know the amount of classes that will be created ?....

What am I trying to do:
I have a process that scans a directory for files tranxxxxx.txt
for each transaction file I want to create a clsTransactionHandler
the class will read the file, process it, store it where it should etc etc
for each class the process needs to know when it is done (to notify the
user)...

Please advise

TIA
Guy Cohen

Dim list As New List(Of clsTransactionHandler)
Dim cls as clsTransactionHandler

' For each class that you instantiate:
cls = New clsTransactionHandler
AddHandler cls.SomeEvent, AddressOf SomeEventHandler
list.Add(cls)


Private Sub SomeEventHandler()
....


To prevent leaking memory, be sure to call RemoveHandler when you free
each clsTransactionHandler object.
 
C

CMoya

Another way in additon to what others have suggested is create a custom
collection instead of an array that will funnel and raise the events
Look up how to create a custom collection class
(http://visualstudiomagazine.com/features/article.aspx?editorialsid=1329).

That way you could use
Dim WithEvents myCol As New MyCollection

The collection class would look something like this
(not complete)
Public Class MyCollection(Of T)
Inherits System.Collections.CollectionBase

Public Event MySpecialEvent(ByVal sender As Object, ByVal e As
EventArgs)

Protected Overrides Sub OnInsertComplete(ByVal index As Integer, ByVal
value As Object)
AddHandler value.MySpecialEvent, AddressOf HandleMySpecialEvent
MyBase.OnInsertComplete(index, value)
End Sub

Protected Overrides Sub OnRemoveComplete(ByVal index As Integer, ByVal
value As Object)
RemoveHandler value.MySpecialEvent, AddressOf HandleMySpecialEvent
MyBase.OnRemoveComplete(index, value)
End Sub

Private Sub HandleMySpecialEvent(ByVal sender As Object, ByVal e As
EventArgs)
'Bubble up the event
RaiseEvent MySpecialEvent(sender, e)
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top